<?php
namespace app\index\controller;


use app\index\model\AccountModel;
use app\index\model\BondFanModel;
use app\index\model\BondJiaoModel;
use app\index\model\BondKaiModel;
use app\index\model\BondShouModel;
use app\index\model\BondTuiModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertDownListModel;
use app\index\model\CertDownModel;
use app\index\model\CertEmpbuModel;
use app\index\model\CertEmployModel;
use app\index\model\CertModel;
use app\index\model\DaiKouQiListModel;
use app\index\model\DaiKouQiModel;
use app\index\model\DealbianListModel;
use app\index\model\DealbianModel;
use app\index\model\DeviceTuiListModel;
use app\index\model\DeviceTuiModel;
use app\index\model\DeviceYunListModel;
use app\index\model\DeviceYunModel;
use app\index\model\EnginStampListModel;
use app\index\model\EnginStampModel;
use app\index\model\LabourXiangListModel;
use app\index\model\LabourXiangModel;
use app\index\model\PersonModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractBuyListModel;
use app\index\model\ContractBuyModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractDayListModel;
use app\index\model\ContractDayModel;
use app\index\model\ContractLianModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\DangJieListModel;
use app\index\model\DangJieModel;
use app\index\model\DangModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceJinModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDiaryModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginSpeedModel;
use app\index\model\FileModel;
use app\index\model\FlowLogModel;
use app\index\model\FlowSetModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourYongListModel;
use app\index\model\LabourYongModel;
use app\index\model\OptionFeiModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxListModel;
use app\index\model\PiaoTaxModel;
use app\index\model\SealGaiListModel;
use app\index\model\SealGaiModel;
use app\index\model\SealModel;
use app\index\model\SealWaiModel;
use app\index\model\SealZhiModel;
use app\index\model\ShouHuanModel;
use app\index\model\ShouQiListModel;
use app\index\model\ShouQiModel;
use app\index\model\ShouXiangKouModel;
use app\index\model\ShouXiangListModel;
use app\index\model\ShouXiangModel;
use app\index\model\SpeedMonthModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\TongyongModel;
use app\index\model\ShouTouListModel;
use app\index\model\ShouTouModel;
use app\index\model\WageListModel;
use app\index\model\WageModel;
use app\index\model\ZhiDaybaoListModel;
use app\index\model\ZhiDaybaoModel;
use app\index\model\ZhiDayfuListModel;
use app\index\model\ZhiDayfuModel;
use app\index\model\ZhiEnbaoListModel;
use app\index\model\ZhiEnbaoModel;
use app\index\model\ZhiJieModel;
use app\index\model\ZhiJintuiListModel;
use app\index\model\ZhiJintuiModel;
use app\index\model\ZhiLianjieModel;
use app\index\model\ZhiTouListModel;
use app\index\model\ZhiTouModel;
use app\index\model\EnginVisaModel;
use app\index\model\WuCaiListModel;
use app\index\model\WuCaiModel;
use app\index\model\WuChuModel;
use app\index\model\WuLingListModel;
use app\index\model\WuLingModel;
use app\index\model\WuRuListModel;
use app\index\model\ZhiXiangListModel;
use app\index\model\ZhiXiangModel;
use app\index\service\BianhaoService;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\LabourService;
use app\index\service\OptService;
use app\index\service\PiaoService;
use app\index\service\SceneService;
use app\index\service\StuffService;
use app\index\service\UserService;
use app\index\service\XingService;
use app\index\service\ZhiService;
use think\Db;
use think\Exception;
use think\facade\Config;
use think\Request;

class Flowdata extends Common
{
    public function add_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $param['name'] = trim($param['name']);
            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入项目名称');
                }
                if((new EnginService())->check_engin_name($param['name'])){
                    return outputJson(4000, '项目名称已存在');
                }
                if(empty($param['engintype'])){
                    return outputJson(4000, '请选择项目类型');
                }
                if(empty($param['zhaotype'])){
                    return outputJson(4000, '请选择招标方式');
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择项目所属单位');
                }
                if(empty($param['business'])){
                    return outputJson(4000, '请选择项目属性');
                }
                if($param['business'] == 2){
                    if(empty($param['lian_unit'])){
                        return outputJson(4000, '请选择联营方/委托方');
                    }
                }else{
                    $param['lian_unit'] = '';
                }
                if(empty($param['touunit'])){
                    return outputJson(4000, '请选择参投单位');
                }
                if($param['kaiguo'] != '中标'){
                    $param['winunit'] = '';
                    $param['winfee'] = '';
                    $param['windt'] = '';
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginModel::insertGetId($param);

                (new FlowService())->save_bill('engin', $id);
                (new OptService())->relation_file($files,'engin',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin', $id, '项目');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
            $data['areas'] = (new OptService())->get_city_cader();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin');
            $data['files'] = (new OptService())->get_file('engin');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin');
            $data['isnext'] = (new FlowService())->check_isnext('engin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['name'] = trim($param['name']);
            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入项目名称');
                }
                if((new EnginService())->check_engin_name($param['name'], $param['id'])){
                    return outputJson(4000, '项目名称已存在');
                }
                if(empty($param['engintype'])){
                    return outputJson(4000, '请选择项目类型');
                }
                if(empty($param['zhaotype'])){
                    return outputJson(4000, '请选择招标方式');
                }
                if(empty($param['chanid'])){
                    return outputJson(4000, '请选择项目所属单位');
                }
                if(empty($param['business'])){
                    return outputJson(4000, '请选择项目属性');
                }
                if($param['business'] == 2){
                    if(empty($param['lian_unit'])){
                        return outputJson(4000, '请选择联营方/委托方');
                    }
                }else{
                    $param['lian_unit'] = '';
                }
                if(empty($param['touunit'])){
                    return outputJson(4000, '请选择参投单位');
                }
                if($param['kaiguo'] != '中标'){
                    $param['winunit'] = '';
                    $param['winfee'] = '';
                    $param['windt'] = '';
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin', $param['id']);

                }else{
                    (new FlowService())->save_bill('engin', $param['id']);
                }

                (new OptService())->relation_file($files,'engin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin', $param['id'], '项目');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['data']['touunit_info'] = (new UserService())->get_users_part_arr($data['data']['touunit'], 'dept');
            $data['data']['winunit_info'] = (new UserService())->get_users_part_arr($data['data']['winunit'], 'dept');
            $data['data']['bianer_info'] = (new UserService())->get_users_part_arr($data['data']['bianer'], 'user');
            $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
            $data['areas'] = (new OptService())->get_city_cader();


            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_shou_tou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['xmsr_tbf']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_shou_tou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'shou_tou') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                $id = ShouTouModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shouid'] = $id;
                    ShouTouListModel::insert($vv);
                }
                (new FlowService())->save_bill('shou_tou', $id);

                (new OptService())->relation_file($files,'shou_tou',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'shou_tou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'shou_tou', $id, '投标费收取');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_shou_tou_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_tbf']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('shou_tou');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('shou_tou');
            $data['isadvan'] = (new FlowService())->check_isadvan('shou_tou');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('shou_tou');
            $data['isnext'] = (new FlowService())->check_isnext('shou_tou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_shou_tou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'shou_tou') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                ShouTouModel::update($param);

                ShouTouListModel::where(['shouid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shouid'] = $param['id'];
                    ShouTouListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('shou_tou', $param['id']);
                }else{
                    (new FlowService())->save_bill('shou_tou', $param['id']);
                }

                (new OptService())->relation_file($files,'shou_tou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'shou_tou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'shou_tou', $param['id'], '投标费收取');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ShouTouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $list = ShouTouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_tbf']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('shou_tou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('shou_tou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('shou_tou',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('shou_tou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('shou_tou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_tou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['xmzc_tbf']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_zhi_tou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_tou') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                $id = ZhiTouModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    ZhiTouListModel::insert($vv);
                }
                (new FlowService())->save_bill('zhi_tou', $id);

                (new OptService())->relation_file($files,'zhi_tou',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_tou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_tou', $id, '投标费支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_tou_number();
            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmzc_tbf']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_tou');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_tou');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_tou');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_tou');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_tou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_tou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_tou') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                ZhiTouModel::update($param);

                ZhiTouListModel::where(['zhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    ZhiTouListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_tou', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_tou', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_tou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_tou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_tou', $param['id'], '投标费支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiTouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = ZhiTouListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmzc_tbf']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_tou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_tou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_tou',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_tou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_tou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dealcom') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) > 0){
                    $isdea = 0;
                    foreach ($list as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '账户信息,请选择默认账户');
                    }
                }
            }

            Db::startTrans();
            try {
                $id = DealcomModel::insertGetId($param);


                foreach($list as $vv){
                    $vv['dealid'] = $id;
                    DealcomListModel::insert($vv);
                }

                (new FinanceService())->set_dealcom_zhanginfo($id);

                (new FlowService())->save_bill('dealcom', $id);
                (new OptService())->relation_file($files,'dealcom',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $id, '往来单位');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('dealcom');
            $data['flowsetname'] = FlowSetModel::get_name('dealcom');
            $data['isadvan'] = (new FlowService())->check_isadvan('dealcom');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('dealcom');
            $data['isnext'] = (new FlowService())->check_isnext('dealcom');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['id'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dealcom') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) > 0){
                    $isdea = 0;
                    foreach ($list as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '账户信息,请选择默认账户');
                    }
                }
            }


            Db::startTrans();
            try {
                DealcomModel::update($param);

                DealcomListModel::where(['dealid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dealid'] = $param['id'];
                    DealcomListModel::insert($vv);
                }

                (new FinanceService())->set_dealcom_zhanginfo($param['id']);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dealcom', $param['id']);
                }else{
                    (new FlowService())->save_bill('dealcom', $param['id']);
                }

                (new OptService())->relation_file($files,'dealcom',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $param['id'], '往来单位');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $list = DealcomListModel::where(['dealid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['isdea_name'] = $item['isdea'] > 0 ? '是' : '否';
            }
            $data['list'] = $list;



            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');


            $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dealcom');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dealcom',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('dealcom',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dealcom');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dealbian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['mid'])){
                    return outputJson(4000, '请选择变更往来单位');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['mid'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dealbian') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) > 0){
                    $isdea = 0;
                    foreach ($list as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '账户信息,请选择默认账户');
                    }
                }
            }


            Db::startTrans();
            try {
                $id = DealbianModel::insertGetId($param);


                foreach($list as $vv){
                    $vv['dealid'] = $id;
                    DealbianListModel::insert($vv);
                }

                (new FinanceService())->set_dealbian_zhanginfo($id);

                (new FlowService())->save_bill('dealbian', $id);


                $chuFiles = [];
                foreach ($files as $vvv){
                    $ffinfo = FileModel::where(['id'=>$vvv['id']])->find()->toArray();
                    if($ffinfo['table'] == 'dealcom'){
                        $newArr = $ffinfo;
                        $newArr['table'] = '';
                        $newArr['mid'] = 0;
                        $newArr['created_user'] = $param['created_user'];
                        $newArr['created_at'] = $param['created_at'];
                        unset($newArr['id']);

                        $ins_id = FileModel::insertGetId($newArr);
                        $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
                        if(FileModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]) === false){
                            throw new Exception("附件处理失败,请重新提交");
                        }
                        $chuFiles[] = [
                            'id'=>$ins_id,
                            'typeid'=>$vvv['typeid'],
                            'fileplain'=>$vvv['fileplain'],
                        ];
                    }else{
                        $chuFiles[] = $vvv;
                    }
                }
                (new OptService())->relation_file($chuFiles,'dealbian',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealbian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealbian', $id, '往来单位变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('dealbian');
            $data['flowsetname'] = FlowSetModel::get_name('dealbian');
            $data['isadvan'] = (new FlowService())->check_isadvan('dealbian');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('dealbian');
            $data['isnext'] = (new FlowService())->check_isnext('dealbian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealbian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['mid'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dealbian') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) > 0){
                    $isdea = 0;
                    foreach ($list as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '账户信息,请选择默认账户');
                    }
                }
            }


            Db::startTrans();
            try {
                DealbianModel::update($param);

                DealbianListModel::where(['dealid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dealid'] = $param['id'];
                    DealbianListModel::insert($vv);
                }

                (new FinanceService())->set_dealbian_zhanginfo($param['id']);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dealbian', $param['id']);
                }else{
                    (new FlowService())->save_bill('dealbian', $param['id']);
                }



                $chuFiles = [];
                foreach ($files as $vvv){
                    $ffinfo = FileModel::where(['id'=>$vvv['id']])->find()->toArray();
                    if($ffinfo['table'] == 'dealcom'){
                        $newArr = $ffinfo;
                        $newArr['table'] = '';
                        $newArr['mid'] = 0;
                        $newArr['created_user'] = $param['updated_user'];
                        $newArr['created_at'] = $param['updated_at'];
                        unset($newArr['id']);

                        $ins_id = FileModel::insertGetId($newArr);
                        $encpath =  encrypt($ins_id, 'E', Config::get('service.info_key'));
                        if(FileModel::where(['id'=>$ins_id])->update(['encpath'=>$encpath]) === false){
                            throw new Exception("附件处理失败,请重新提交");
                        }
                        $chuFiles[] = [
                            'id'=>$ins_id,
                            'typeid'=>$vvv['typeid'],
                            'fileplain'=>$vvv['fileplain'],
                        ];
                    }else{
                        $chuFiles[] = $vvv;
                    }
                }
                (new OptService())->relation_file($files,'dealbian',$param['id']);


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealbian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealbian', $param['id'], '往来单位变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealbianModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['midinfo'] = DealcomModel::where(['id'=>$data['data']['mid']])->find();


            $list = DealbianListModel::where(['dealid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['isdea_name'] = $item['isdea'] > 0 ? '是' : '否';
            }
            $data['list'] = $list;

            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');


            $data['files'] = (new OptService())->get_file('dealbian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dealbian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dealbian',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('dealbian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dealbian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['shihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['faer'])){
                    return outputJson(4000, '请选择(发包方)甲方');
                }
                if(empty($param['chenger'])){
                    return outputJson(4000, '请选择承包方(乙方)');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入合同金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }


            $param['number'] = (new BianhaoService())->create_contract_shi_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_shi') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractShiModel::insertGetId($param);

                (new FlowService())->save_bill('contract_shi', $id);
                (new OptService())->relation_file($files,'contract_shi',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_shi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_shi', $id, '收入合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_shi_number();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_shi');
            $data['files'] = (new OptService())->get_file('contract_shi');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_shi');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_shi');
            $data['isnext'] = (new FlowService())->check_isnext('contract_shi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['shihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['faer'])){
                    return outputJson(4000, '请选择(发包方)甲方');
                }
                if(empty($param['chenger'])){
                    return outputJson(4000, '请选择承包方(乙方)');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入合同金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_shi') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ContractShiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_shi', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_shi', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_shi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi', $param['id'], '收入合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['chenger_info'] = (new UserService())->get_users_part_arr($data['data']['chenger'], 'dept');
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');

            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_shi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_shi',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_shi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_shi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择主合同');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入补充合同名称');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入合同金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                $shi_enginid = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');
                if($param['enginid'] != $shi_enginid){
                    return outputJson(4000, '项目名称与主合同关联项目不匹配，请重新选择');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_bu') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractBuModel::insertGetId($param);

                (new FlowService())->save_bill('contract_bu', $id);
                (new OptService())->relation_file($files,'contract_bu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bu', $id, '补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $shiid = $request->param('shiid', 0, 'int');
            $shiinfo = ContractShiModel::where(['id'=>$shiid])->find();
            if($shiinfo){
                $data['engininfo'] = EnginModel::get_info_by_pk($shiinfo['enginid']);
            }
            $data['shiinfo'] = $shiinfo;

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_bu');
            $data['files'] = (new OptService())->get_file('contract_bu');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_bu');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_bu');
            $data['isnext'] = (new FlowService())->check_isnext('contract_bu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择主合同');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入补充合同名称');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入合同金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                $shi_enginid = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');
                if($param['enginid'] != $shi_enginid){
                    return outputJson(4000, '项目名称与主合同关联项目不匹配，请重新选择');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_bu') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                ContractBuModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_bu', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_bu', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_bu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bu', $param['id'], '补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shiinfo = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();
            if($shiinfo){
                $data['engininfo'] = EnginModel::get_info_by_pk($shiinfo['enginid']);
            }
            $data['shiinfo'] = $shiinfo;
            $data['files'] = (new OptService())->get_file('contract_bu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_bu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_bu',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_bu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_bu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_visa(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['visatype'])){
                    return outputJson(4000, '请选择签证类别');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入签证主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入签证金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
            }


            $param['number'] = (new BianhaoService())->create_engin_visa_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_visa') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginVisaModel::insertGetId($param);


                (new FlowService())->save_bill('engin_visa', $id);

                (new OptService())->relation_file($files,'engin_visa',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_visa', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_visa', $id, '项目签证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_engin_visa_number();
            $data['visatype'] = (new OptService())->get_base_info('scene_visatype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_visa');
            $data['files'] = (new OptService())->get_file('engin_visa');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_visa');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_visa');
            $data['isnext'] = (new FlowService())->check_isnext('engin_visa');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_visa(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['visatype'])){
                    return outputJson(4000, '请选择签证类别');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入签证主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入签证金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_visa') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginVisaModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_visa', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_visa', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_visa',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_visa', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_visa', $param['id'], '项目签证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginVisaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['visatype'] = (new OptService())->get_base_info('scene_visatype');
            $data['files'] = (new OptService())->get_file('engin_visa', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_visa');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_visa',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_visa',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_visa');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_chan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择产值月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入月报主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本月产值金额');
                }

                if((new SceneService())->check_engin_chan_month($param['month'], $param['enginid'])){
                    return outputJson(4000, '该项目产值月份已存在');
                }
            }


            $param['number'] = (new BianhaoService())->create_engin_chan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_chan') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = EnginChanModel::insertGetId($param);


                (new FlowService())->save_bill('engin_chan', $id);

                (new OptService())->relation_file($files,'engin_chan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_chan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_chan', $id, '产值月报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_engin_chan_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_chan');
            $data['files'] = (new OptService())->get_file('engin_chan');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_chan');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_chan');
            $data['isnext'] = (new FlowService())->check_isnext('engin_chan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_chan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择产值月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入月报主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本月产值金额');
                }

                if((new SceneService())->check_engin_chan_month($param['month'], $param['enginid'], $param['id'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_chan') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginChanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_chan', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_chan', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_chan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_chan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_chan', $param['id'], '产值月报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginChanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['files'] = (new OptService())->get_file('engin_chan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_chan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_chan',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_chan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_chan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_shi_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择收入合同名称');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择结算类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次结算金额');
                }
                if($param['koufafee'] == ''){
                    return outputJson(4000, '请输入扣罚款金额');
                }
            }


            $param['number'] = (new BianhaoService())->create_contract_shi_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_shi_jie') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractShiJieModel::insertGetId($param);

                (new FlowService())->save_bill('contract_shi_jie', $id);
                (new OptService())->relation_file($files,'contract_shi_jie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_shi_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_shi_jie', $id, '收入合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_shi_jie_number();
            $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_shi_jie');
            $data['files'] = (new OptService())->get_file('contract_shi_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_shi_jie');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_shi_jie');
            $data['isnext'] = (new FlowService())->check_isnext('contract_shi_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_shi_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择收入合同名称');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择结算类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次结算金额');
                }
                if($param['koufafee'] == ''){
                    return outputJson(4000, '请输入扣罚款金额');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_shi_jie') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ContractShiJieModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_shi_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_shi_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_shi_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi_jie', $param['id'], '收入合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['shiinfo'] = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();
            $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();

            $data['files'] = (new OptService())->get_file('contract_shi_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_shi_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_shi_jie',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_shi_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_shi_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_shou_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['feilei'])) {
                    return outputJson(4000, '请选择款项类型');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }

                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engininfo['business'] == 2){
                    if (empty($param['iskou'])) {
                        return outputJson(4000, '请选择本次收款是否扣收');
                    }
                }else{
                    $param['iskou'] = '';
                }
            }

            $param['number'] = (new BianhaoService())->create_shou_xiang_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'add_shou_xiang') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入收款明细');
                }
            }

            if(!isset($param['kou'])){
                $param['kou'] = [];
            }
            $kou = array_filter($param['kou']);
            unset($param['kou']);
            if($param['isturn'] > 0){
                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engininfo['business'] == 2 && $param['iskou'] == '是'){
                    if(count($kou) < 1){
                        return outputJson(4000, '请输入扣收明细');
                    }
                }else{
                    $kou = [];
                }
            }


            Db::startTrans();
            try {
                $id = ShouXiangModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shouid'] = $id;
                    ShouXiangListModel::insert($vv);
                }
                foreach($kou as $vv){
                    $vv['shouid'] = $id;
                    $vv['enginid'] = $param['enginid'];
                    ShouXiangKouModel::insert($vv);
                }

                (new FlowService())->save_bill('shou_xiang', $id);

                (new OptService())->relation_file($files,'shou_xiang',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'shou_xiang', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'shou_xiang', $id, '项目收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_shou_xiang_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['feilei'] = (new UserService())->get_option_fei_slet(['xmsr_gchk','xmsr_gsdz','xmsr_lyfbyj']);
            $data['kou_feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('shou_xiang');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('shou_xiang');
            $data['isadvan'] = (new FlowService())->check_isadvan('shou_xiang');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('shou_xiang');
            $data['isnext'] = (new FlowService())->check_isnext('shou_xiang');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_shou_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['feilei'])) {
                    return outputJson(4000, '请选择款项类型');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engininfo['business'] == 2){
                    if (empty($param['iskou'])) {
                        return outputJson(4000, '请选择本次收款是否扣收');
                    }
                }else{
                    $param['iskou'] = '';
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'add_shou_xiang') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入收款明细');
                }
            }


            if(!isset($param['kou'])){
                $param['kou'] = [];
            }
            $kou = array_filter($param['kou']);
            unset($param['kou']);
            if($param['isturn'] > 0){
                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engininfo['business'] == 2 && $param['iskou'] == '是'){
                    if(count($kou) < 1){
                        return outputJson(4000, '请输入扣收明细');
                    }
                }else{
                    $kou = [];
                }
            }


            Db::startTrans();
            try {
                ShouXiangModel::update($param);

                ShouXiangListModel::where(['shouid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shouid'] = $param['id'];
                    ShouXiangListModel::insert($vv);
                }

                ShouXiangKouModel::where(['shouid'=>$param['id']])->delete();
                foreach($kou as $vv){
                    $vv['shouid'] = $param['id'];
                    $vv['enginid'] = $param['enginid'];
                    ShouXiangKouModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('shou_xiang', $param['id']);
                }else{
                    (new FlowService())->save_bill('shou_xiang', $param['id']);
                }

                (new OptService())->relation_file($files,'shou_xiang',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'shou_xiang', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'shou_xiang', $param['id'], '项目收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ShouXiangModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $list = ShouXiangListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item1){
                $item1['feitype_name'] = OptionFeiModel::where(['id'=>$item1['feitype']])->value('name');
            }
            $data['list'] = $list;

            $kou = ShouXiangKouModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($kou as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['kou'] = $kou;

            $data['feilei'] = (new UserService())->get_option_fei_slet(['xmsr_gchk','xmsr_gsdz','xmsr_lyfbyj']);
            $data['kou_feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('shou_xiang', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('shou_xiang');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('shou_xiang',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('shou_xiang',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('shou_xiang');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['piaotype'])){
                    return outputJson(4000, '请选择发票类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入开票主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次开票金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择受票单位');
                }
                if(empty($param['kaier'])){
                    return outputJson(4000, '请选择开票单位');
                }

                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engininfo['business'] == 2){
                    if($param['yingshuifee'] == ''){
                        return outputJson(4000, '请输入本次开票应计税费');
                    }
                    if(empty($param['shuiqutype'])){
                        return outputJson(4000, '请选择税费收取方式');
                    }else{
                        if($param['shuiqutype'] == 3){
                            if(empty($param['kouid'])){
                                return outputJson(4000, '请选择关联费用扣收');
                            }
                        }
                    }
                }
            }

            $param['nofee'] = $param['money'] - $param['ratefee'];
            $param['number'] = (new BianhaoService())->create_piao_kai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'piao_kai') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = PiaokaiModel::insertGetId($param);

                (new FlowService())->save_bill('piao_kai', $id);

                (new OptService())->relation_file($files,'piao_kai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_kai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_kai', $id, '项目开票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['shuiqutype'] = (new PiaoService())->get_kai_shuiqutype();
            $data['number'] = (new BianhaoService())->create_piao_kai_number();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('piao_kai');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_kai');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('piao_kai');
            $data['isnext'] = (new FlowService())->check_isnext('piao_kai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['files'] = (new OptService())->get_file('piao_kai');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['piaotype'])){
                    return outputJson(4000, '请选择发票类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入开票主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次开票金额含税');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择受票单位');
                }
                if(empty($param['kaier'])){
                    return outputJson(4000, '请选择开票单位');
                }

                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($engininfo['business'] == 2){
                    if($param['yingshuifee'] == ''){
                        return outputJson(4000, '请输入本次开票应计税费');
                    }
                    if(empty($param['shuiqutype'])){
                        return outputJson(4000, '请选择税费收取方式');
                    }else{
                        if($param['shuiqutype'] == 3){
                            if(empty($param['kouid'])){
                                return outputJson(4000, '请选择关联费用扣收');
                            }
                        }
                    }
                }

            }

            $param['nofee'] = $param['money'] - $param['ratefee'];
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'piao_kai') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                PiaokaiModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_kai', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_kai', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_kai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_kai', $param['id'], '项目开票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['kouidinfo'] = ShouXiangKouModel::alias('list')
                ->field('shou.number,list.id,list.money')
                ->join('shou_xiang shou','shou.id=list.shouid')
                ->where(['list.id'=>$data['data']['kouid']])->find();


            $data['data']['kaier_info'] = (new UserService())->get_users_part_arr($data['data']['kaier'], 'dept');
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['shuiqutype'] = (new PiaoService())->get_kai_shuiqutype();


            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_kai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_kai',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('piao_kai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_kai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_budget(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入预算主题');
                }

                $exist = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->find();
                if($exist){
                    return outputJson(4000, '该项目下存在预算，请勿重复添加');
                }
            }

            $param['number'] = (new BianhaoService())->create_engin_budget_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_budget') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预算明细');
                }
            }

            Db::startTrans();
            try {
                $id = EnginBudgetModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['budgetid'] = $id;
                    EnginBudgetListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_budget', $id);

                (new OptService())->relation_file($files,'engin_budget',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_budget', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_budget', $id, '预算编制');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_engin_budget_number();
            $data['bianer'] = $this->userinfo['realname'];
            $data['biandt'] = date("Y-m-d");
            $data['budtype'] = (new EnginService())->get_engin_budget_budtype();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_budget');
            $data['files'] = (new OptService())->get_file('engin_budget');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_budget');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_budget');
            $data['isnext'] = (new FlowService())->check_isnext('engin_budget');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_budget(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入预算主题');
                }

                $exist = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目下存在预算，请勿重复添加');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_budget') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预算明细');
                }
            }


            Db::startTrans();
            try {
                EnginBudgetModel::update($param);

                EnginBudgetListModel::where(['budgetid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['budgetid'] = $param['id'];
                    EnginBudgetListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_budget', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_budget', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_budget',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_budget', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_budget', $param['id'], '预算编制');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBudgetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['budtype'] = (new EnginService())->get_engin_budget_budtype();
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $list = EnginBudgetListModel::where(['budgetid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item['budtype']);
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('engin_budget', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_budget');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_budget',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_budget',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_budget');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_budbian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['budgetid'])){
                    return outputJson(4000, '请选择预算主题');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入本次变更主题');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择本次变更类型');
                }

                $budgetInfo = EnginBudgetModel::where(['id'=>$param['budgetid']])->find();
                if($budgetInfo['enginid'] != $param['enginid']){
                    return outputJson(4000, '项目名称与预算主题关联项目不匹配，请重新选择');
                }
                if($budgetInfo['deleted_user'] != 0 || $budgetInfo['isturn'] != 1 || $budgetInfo['status'] != 1){
                    return outputJson(4000, '该预算已不存在，请重新选择');
                }
                $exist = EnginBudbianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'budgetid'=>$param['budgetid']])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }

            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_budbian') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $tiaolist = array_filter($param['tiaolist']);
            unset($param['tiaolist']);
            $zenglist = array_filter($param['zenglist']);
            unset($param['zenglist']);

            if($param['isturn'] > 0){
                if($param['type'] == 1 || $param['type'] == 3){
                    if(count($tiaolist) < 1){
                        return outputJson(4000, '请输入调整明细');
                    }
                }
                if($param['type'] == 2 || $param['type'] == 3){
                    if(count($zenglist) < 1){
                        return outputJson(4000, '请输入增项明细');
                    }
                }
            }


            Db::startTrans();
            try {
                $id = EnginBudbianModel::insertGetId($param);

                foreach($tiaolist as $vv){
                    $vv['budbianid'] = $id;
                    EnginBudbianListModel::insert($vv);
                }

                foreach($zenglist as $vv){
                    $vv['budbianid'] = $id;
                    EnginBudbianListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_budbian', $id);
                (new OptService())->relation_file($files,'engin_budbian',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_budbian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_budbian', $id, '预算变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $budgetid = $request->param('budgetid', 0, 'int');


            $data['engininfo'] = [];
            $data['budgetinfo'] = EnginBudgetModel::get_info_by_pk($budgetid);
            if($data['budgetinfo']){
                $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['budgetinfo']['enginid']);
            }

            $data['budtype'] = (new EnginService())->get_engin_budget_budtype();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_budbian');
            $data['files'] = (new OptService())->get_file('engin_budbian');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_budbian');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_budbian');
            $data['isnext'] = (new FlowService())->check_isnext('engin_budbian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_budbian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['budgetid'])){
                    return outputJson(4000, '请选择预算主题');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入本次变更主题');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择本次变更类型');
                }

                $budgetInfo = EnginBudgetModel::where(['id'=>$param['budgetid']])->find();
                if($budgetInfo['enginid'] != $param['enginid']){
                    return outputJson(4000, '项目名称与预算主题关联项目不匹配，请重新选择');
                }
                if($budgetInfo['deleted_user'] != 0 || $budgetInfo['isturn'] != 1 || $budgetInfo['status'] != 1){
                    return outputJson(4000, '该预算已不存在，请重新选择');
                }
                $exist = EnginBudbianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'budgetid'=>$param['budgetid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }

            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_budbian') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $tiaolist = array_filter($param['tiaolist']);
            unset($param['tiaolist']);
            $zenglist = array_filter($param['zenglist']);
            unset($param['zenglist']);

            if($param['isturn'] > 0){
                if($param['type'] == 1 || $param['type'] == 3){
                    if(count($tiaolist) < 1){
                        return outputJson(4000, '请输入调整明细');
                    }
                }
                if($param['type'] == 2 || $param['type'] == 3){
                    if(count($zenglist) < 1){
                        return outputJson(4000, '请输入增项明细');
                    }
                }
            }


            Db::startTrans();
            try {
                EnginBudbianModel::update($param);


                EnginBudbianListModel::where(['budbianid'=>$param['id']])->delete();
                foreach($tiaolist as $vv){
                    $vv['budbianid'] = $param['id'];
                    EnginBudbianListModel::insert($vv);
                }

                foreach($zenglist as $vv){
                    $vv['budbianid'] = $param['id'];
                    EnginBudbianListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_budbian', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_budbian', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_budbian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_budbian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_budbian', $param['id'], '成本预算变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginBudbianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = [];
            $data['budgetinfo'] = EnginBudgetModel::get_info_by_pk($data['data']['budgetid']);
            if($data['budgetinfo']){
                $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['budgetinfo']['enginid']);
            }

            $data['budtype'] = (new EnginService())->get_engin_budget_budtype();

            $tiaolist = EnginBudbianListModel::where(['budbianid'=>$data['data']['id']])->where('listid','gt',0)->select()->toArray();
            foreach ($tiaolist as &$item_tiao){
                $item_tiao['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item_tiao['budtype']);
            }
            $data['tiaolist'] = $tiaolist;

            $zenglist = EnginBudbianListModel::where(['budbianid'=>$data['data']['id']])->where('listid','=',0)->select()->toArray();
            foreach ($zenglist as &$item){
                $item['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item['budtype']);
            }
            $data['zenglist'] = $zenglist;

            $data['files'] = (new OptService())->get_file('engin_budbian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_budbian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_budbian',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_budbian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_budbian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['caishu'])){
                    return outputJson(4000, '请选择合同属性');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择采购方(甲方)');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }

            $param['number'] = (new BianhaoService())->create_contract_cai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_cai') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }


            $param['listnum'] = 0;
            foreach($list as $val){
                $param['listnum'] += $val['num'];
            }


            Db::startTrans();
            try {
                $id = ContractCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    $vv['shengnum'] = $vv['num'];
                    ContractCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_cai', $id);

                (new OptService())->relation_file($files,'contract_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_cai', $id, '支出合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_cai_number();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['caitype'] = (new ContractService())->get_contract_cai_caitype();
            $data['caishu'] = (new ContractService())->get_contract_cai_caishu();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_cai');
            $data['files'] = (new OptService())->get_file('contract_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_cai');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_cai');
            $data['isnext'] = (new FlowService())->check_isnext('contract_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractCaiModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['caishu'])){
                    return outputJson(4000, '请选择合同属性');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择采购方(甲方)');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_cai') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }

            $param['listnum'] = 0;
            foreach($list as $val){
                $param['listnum'] += $val['num'];
            }

            Db::startTrans();
            try {
                ContractCaiModel::update($param);


                ContractCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    $vv['shengnum'] = $vv['num'];
                    ContractCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_cai', $param['id'], '支出合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');
            $data['data']['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');

            $list = ContractCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['listid_name'] = EnginBudgetListModel::where(['id'=>$item['listid']])->value('name');
            }
            $data['list'] = $list;


            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['caitype'] = (new ContractService())->get_contract_cai_caitype();
            $data['caishu'] = (new ContractService())->get_contract_cai_caishu();

            $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_cai',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入变更主题');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择变更签订日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }

                $cai_enginid = ContractCaiModel::where(['id'=>$param['caiid']])->value('enginid');
                if($param['enginid'] != $cai_enginid){
                    return outputJson(4000, '项目名称与合同名称关联项目不匹配，请重新选择');
                }

                $exist = ContractBianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'caiid'=>$param['caiid']])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_bian') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入变更明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = ContractCaiListModel::where(['caiid'=>$param['caiid']])
                        ->where(['name'=>$bbb['name'],'unit'=>$bbb['unit']])->find();
                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行明细信息已存在");
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                $id = ContractBianModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['bianid'] = $id;
                    ContractBianListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_bian', $id);
                (new OptService())->relation_file($files,'contract_bian',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bian', $id, '支出合同变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $caiid = $request->param('caiid', 0, 'int');


            $data['engininfo'] = [];
            $data['caiinfo'] = ContractCaiModel::where(['id'=>$caiid])->find();
            if($data['caiinfo']){
                $data['caiinfo']['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($data['caiinfo']['caitype']);
            }
            if($data['caiinfo']){
                $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['caiinfo']['enginid']);
                if($data['engininfo']){
                    $data['engininfo']['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['engininfo']['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');
                }
            }

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_bian');
            $data['files'] = (new OptService())->get_file('contract_bian');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_bian');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_bian');
            $data['isnext'] = (new FlowService())->check_isnext('contract_bian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入变更主题');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择变更签订日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }

                $cai_enginid = ContractCaiModel::where(['id'=>$param['caiid']])->value('enginid');
                if($param['enginid'] != $cai_enginid){
                    return outputJson(4000, '项目名称与合同名称关联项目不匹配，请重新选择');
                }

                $exist = ContractBianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'caiid'=>$param['caiid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_bian') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入变更明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = ContractCaiListModel::where(['caiid'=>$param['caiid']])
                        ->where(['name'=>$bbb['name'],'unit'=>$bbb['unit']])->find();
                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行明细信息已存在");
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                ContractBianModel::update($param);


                ContractBianListModel::where(['bianid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['bianid'] = $param['id'];
                    ContractBianListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_bian', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_bian', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_bian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bian', $param['id'], '支出合同变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            if($data['caiinfo']){
                $data['caiinfo']['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($data['caiinfo']['caitype']);
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['caiinfo']['enginid']);
            if($data['engininfo']){
                $engininfo['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['engininfo']['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');
            }

            $list = ContractBianListModel::where(['bianid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['bglistid_name'] = EnginBudgetListModel::where(['id'=>$item['bglistid']])->find();
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_bian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_bian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_bian',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_bian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_bian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_cai_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择支出合同名称');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择结算类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次结算金额');
                }
                if($param['koufafee'] == ''){
                    return outputJson(4000, '请输入扣罚款金额');
                }
            }


            $param['number'] = (new BianhaoService())->create_contract_cai_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_cai_jie') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractCaiJieModel::insertGetId($param);

                (new FlowService())->save_bill('contract_cai_jie', $id);
                (new OptService())->relation_file($files,'contract_cai_jie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_cai_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_cai_jie', $id, '支出合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_cai_jie_number();
            $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_cai_jie');
            $data['files'] = (new OptService())->get_file('contract_cai_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_cai_jie');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_cai_jie');
            $data['isnext'] = (new FlowService())->check_isnext('contract_cai_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_cai_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择支出合同名称');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择结算类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次结算金额');
                }
                if($param['koufafee'] == ''){
                    return outputJson(4000, '请输入扣罚款金额');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_cai_jie') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ContractCaiJieModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_cai_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_cai_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_cai_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_cai_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_cai_jie', $param['id'], '支出合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['caiinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();

            $data['files'] = (new OptService())->get_file('contract_cai_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_cai_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_cai_jie',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_cai_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_cai_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_buy(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['buytype'])){
                    return outputJson(4000, '请选择采购类型');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
            }

            $param['number'] = (new BianhaoService())->create_contract_buy_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_buy') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }

            $param['listnum'] = 0;
            foreach($list as $val){
                $param['listnum'] += $val['num'];
            }

            Db::startTrans();
            try {
                $id = ContractBuyModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['buyid'] = $id;
                    $vv['shengnum'] = $vv['num'];
                    ContractBuyListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_buy', $id);

                (new OptService())->relation_file($files,'contract_buy',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_buy', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_buy', $id, '采购单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_buy_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['buytype'] = (new ContractService())->get_contract_buy_buytype();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_buy');
            $data['files'] = (new OptService())->get_file('contract_buy');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_buy');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_buy');
            $data['isnext'] = (new FlowService())->check_isnext('contract_buy');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_buy(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['buytype'])){
                    return outputJson(4000, '请选择采购类型');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_buy') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }


            $param['listnum'] = 0;
            foreach($list as $val){
                $param['listnum'] += $val['num'];
            }

            Db::startTrans();
            try {
                ContractBuyModel::update($param);


                ContractBuyListModel::where(['buyid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['buyid'] = $param['id'];
                    $vv['shengnum'] = $vv['num'];
                    ContractBuyListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_buy', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_buy', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_buy',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_buy', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_buy', $param['id'], '采购单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBuyModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = ContractBuyListModel::where(['buyid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['buytype'] = (new ContractService())->get_contract_buy_buytype();

            $data['files'] = (new OptService())->get_file('contract_buy', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_buy');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_buy',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_buy',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_buy');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入收票主题');
                }
            }

            $param['number'] = (new BianhaoService())->create_piao_shou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'piao_shou') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入收票明细');
                }


                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoShouListModel::alias('list')
                        ->join('piao_shou piao','piao.id=list.shouid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }


            $param['ratefee_zhuan'] = 0;
            foreach($list as $val){
                if($val['piaotype'] == 1){
                    $param['ratefee_zhuan'] += $val['ratefee'];
                }
            }


            Db::startTrans();
            try {
                $id = PiaoShouModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shouid'] = $id;
                    PiaoShouListModel::insert($vv);
                }

                (new FlowService())->save_bill('piao_shou', $id);

                (new OptService())->relation_file($files,'piao_shou',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_shou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_shou', $id, '项目收票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_piao_shou_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['yongtype'] = (new PiaoService())->get_piao_shou_yongtype();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('piao_shou');
            $data['files'] = (new OptService())->get_file('piao_shou');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_shou');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('piao_shou');
            $data['isnext'] = (new FlowService())->check_isnext('piao_shou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入收票主题');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'piao_shou') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入收票明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoShouListModel::alias('list')
                        ->join('piao_shou piao','piao.id=list.shouid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where('piao.id','neq',$param['id'])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }

            $param['ratefee_zhuan'] = 0;
            foreach($list as $val){
                if($val['piaotype'] == 1){
                    $param['ratefee_zhuan'] += $val['ratefee'];
                }
            }

            Db::startTrans();
            try {
                PiaoShouModel::update($param);

                PiaoShouListModel::where(['shouid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shouid'] = $param['id'];
                    PiaoShouListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_shou', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_shou', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_shou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_shou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_shou', $param['id'], '项目收票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                $item['yongtype_name'] = (new PiaoService())->get_piao_shou_yongtype_name($item['yongtype']);
            }
            $data['list'] = $list;

            $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
            $data['yongtype'] = (new PiaoService())->get_piao_shou_yongtype();

            $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_shou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_shou',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('piao_shou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_shou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['feilei'])) {
                    return outputJson(4000, '请选择款项类型');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['yiju'])) {
                    return outputJson(4000, '请选择付款依据');
                }
                if($param['yiju'] && $param['yiju'] != 'none'){
                    if (empty($param['yijuid'])) {
                        return outputJson(4000, '请选择依据名称');
                    }
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
                if (empty($param['piaoqing'])) {
                    return outputJson(4000, '请选择票款情况');
                }
                if($param['piaoqing'] == 1){
                    if (empty($param['piaoids'])) {
                        return outputJson(4000, '请选择关联项目收票');
                    }
                }
            }

            $param['caiid'] = 0;
            if($param['yiju'] == 'cai'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'caijie'){
                $param['caiid'] = ContractCaiJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'stuffru'){
                $param['caiid'] = StuffCaiModel::where(['id'=>$param['yijuid']])->value('caiid');
            }

            $param['number'] = (new BianhaoService())->create_zhi_xiang_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_xiang') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                $id = ZhiXiangModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    ZhiXiangListModel::insert($vv);
                }
                (new FlowService())->save_bill('zhi_xiang', $id);

                (new OptService())->relation_file($files,'zhi_xiang',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_xiang', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_xiang', $id, '项目付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_xiang_number();

            $data['feilei'] = (new UserService())->get_option_fei_slet(['xmzc_clf','xmzc_rgf','xmzc_jxsbf','xmzc_zyfbf','xmzc_qtf']);
            $data['yiju'] = (new ZhiService())->get_zhi_xiang_yiju();
            $data['piaoqing'] = (new ZhiService())->get_zhi_xiang_piaoqing();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_xiang');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_xiang');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_xiang');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_xiang');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_xiang');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['feilei'])) {
                    return outputJson(4000, '请选择款项类型');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['yiju'])) {
                    return outputJson(4000, '请选择付款依据');
                }
                if($param['yiju'] && $param['yiju'] != 'none'){
                    if (empty($param['yijuid'])) {
                        return outputJson(4000, '请选择依据名称');
                    }
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
                if (empty($param['piaoqing'])) {
                    return outputJson(4000, '请选择票款情况');
                }
                if($param['piaoqing'] == 1){
                    if (empty($param['piaoids'])) {
                        return outputJson(4000, '请选择关联项目收票');
                    }
                }
            }


            $param['caiid'] = 0;
            if($param['yiju'] == 'cai'){
                $param['caiid'] = $param['yijuid'];
            }elseif($param['yiju'] == 'caijie'){
                $param['caiid'] = ContractCaiJieModel::where(['id'=>$param['yijuid']])->value('caiid');
            }elseif($param['yiju'] == 'stuffru'){
                $param['caiid'] = StuffCaiModel::where(['id'=>$param['yijuid']])->value('caiid');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_xiang') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                ZhiXiangModel::update($param);

                ZhiXiangListModel::where(['zhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    ZhiXiangListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_xiang', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_xiang', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_xiang',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_xiang', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_xiang', $param['id'], '项目付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiXiangModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['yijuidinfo'] = (new ZhiService())->get_zhi_xiang_yijuidinfo($data['data']['yiju'], $data['data']['yijuid']);
            $data['piaoidsinfo'] = PiaoShouListModel::field('id,nums,id as listid')->where('id','in',$data['data']['piaoids'])->select()->toArray();

            $list = ZhiXiangListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feilei'] = (new UserService())->get_option_fei_slet(['xmzc_clf','xmzc_rgf','xmzc_jxsbf','xmzc_zyfbf','xmzc_qtf']);
            $data['yiju'] = (new ZhiService())->get_zhi_xiang_yiju();
            $data['piaoqing'] = (new ZhiService())->get_zhi_xiang_piaoqing();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_xiang', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_xiang');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_xiang',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_xiang',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_xiang');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_enbao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
                if ($param['isdi'] == '是') {
                    if (empty($param['jieid'])) {
                        return outputJson(4000, '请选择关联备用金');
                    }
                }else{
                    $param['jieid'] = 0;
                    $param['jieyufee'] = 0;
                    $param['difee'] = 0;
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['xmzc_qtf']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_zhi_enbao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_enbao') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }

            Db::startTrans();
            try {
                $id = ZhiEnbaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    ZhiEnbaoListModel::insert($vv);
                }
                (new FlowService())->save_bill('zhi_enbao', $id);

                (new OptService())->relation_file($files,'zhi_enbao',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_enbao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_enbao', $id, '项目费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_enbao_number();

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmzc_qtf']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shanginfo'] = ZhiEnbaoModel::where(['created_user'=>$this->userinfo['id'],'deleted_user'=>0,'isturn'=>1])->where('status','in',[0,1])->find();

            $data['files'] = (new OptService())->get_file('zhi_enbao');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_enbao');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_enbao');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_enbao');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_enbao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_enbao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
                if ($param['isdi'] == '是') {
                    if (empty($param['jieid'])) {
                        return outputJson(4000, '请选择关联备用金');
                    }
                }else{
                    $param['jieid'] = 0;
                    $param['jieyufee'] = 0;
                    $param['difee'] = 0;
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_enbao') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }


            Db::startTrans();
            try {
                ZhiEnbaoModel::update($param);

                ZhiEnbaoListModel::where(['zhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    ZhiEnbaoListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_enbao', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_enbao', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_enbao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_enbao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_enbao', $param['id'], '项目费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiEnbaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['jieidinfo'] = ZhiJieModel::where(['id'=>$data['data']['jieid']])->find();

            $list = ZhiEnbaoListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmzc_qtf']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_enbao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_enbao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_enbao',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_enbao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_enbao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_lian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = ContractLianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在联营合同，不能重复创建');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['myer'])){
                    return outputJson(4000, '请选择我方单位');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }


            $param['number'] = (new BianhaoService())->create_contract_lian_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_lian') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractLianModel::insertGetId($param);

                (new FlowService())->save_bill('contract_lian', $id);
                (new OptService())->relation_file($files,'contract_lian',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_lian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_lian', $id, '联营合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_lian_number();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_lian');
            $data['files'] = (new OptService())->get_file('contract_lian');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_lian');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_lian');
            $data['isnext'] = (new FlowService())->check_isnext('contract_lian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_lian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = ContractLianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在联营合同，不能重复创建');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['myer'])){
                    return outputJson(4000, '请选择我方单位');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_lian') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ContractLianModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_lian', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_lian', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_lian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_lian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_lian', $param['id'], '联营合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractLianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['data']['myer_info'] = (new UserService())->get_users_part_arr($data['data']['myer'], 'dept');
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();

            $data['files'] = (new OptService())->get_file('contract_lian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_lian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_lian',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_lian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_lian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dai_kou_qi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入待扣摘要');
                }
            }

            $param['number'] = (new BianhaoService())->create_dai_kou_qi_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dai_kou_qi') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入待扣明细');
                }
            }

            Db::startTrans();
            try {
                $id = DaiKouQiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['kouqiid'] = $id;
                    DaiKouQiListModel::insert($vv);
                }
                (new FlowService())->save_bill('dai_kou_qi', $id);

                (new OptService())->relation_file($files,'dai_kou_qi',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dai_kou_qi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dai_kou_qi', $id, '其他费待扣登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_dai_kou_qi_number();

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks'], ['xmsr_lyfyhks_glf','xmsr_lyfyhks_sf']);

            $data['files'] = (new OptService())->get_file('dai_kou_qi');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('dai_kou_qi');
            $data['isadvan'] = (new FlowService())->check_isadvan('dai_kou_qi');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('dai_kou_qi');
            $data['isnext'] = (new FlowService())->check_isnext('dai_kou_qi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dai_kou_qi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入待扣摘要');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dai_kou_qi') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入待扣明细');
                }
            }


            Db::startTrans();
            try {
                DaiKouQiModel::update($param);

                DaiKouQiListModel::where(['kouqiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['kouqiid'] = $param['id'];
                    DaiKouQiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dai_kou_qi', $param['id']);
                }else{
                    (new FlowService())->save_bill('dai_kou_qi', $param['id']);
                }

                (new OptService())->relation_file($files,'dai_kou_qi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dai_kou_qi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dai_kou_qi', $param['id'], '其他费待扣登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DaiKouQiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = DaiKouQiListModel::where(['kouqiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks'], ['xmsr_lyfyhks_glf','xmsr_lyfyhks_sf']);


            $data['files'] = (new OptService())->get_file('dai_kou_qi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dai_kou_qi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dai_kou_qi',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('dai_kou_qi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dai_kou_qi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_zhi_jintui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }

            $param['feilei'] = (new UserService())->get_optionfeiid_bynum('xmzc_qtf');
            $param['feitype'] = (new UserService())->get_optionfeiid_bynum('xmzc_qtf_jxts');

            $param['number'] = (new BianhaoService())->create_zhi_jintui_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_jintui') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入本次退税进项票明细');
                }
            }

            //本次退税金额合计不可大于剩余可退进项税额！否则无法提交。
            if($param['isturn'] > 0) {
                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($param['money'] > $engininfo['shengjintuifee']){
                    return outputJson(4000, '本次退税金额合计不可大于剩余可退进项税额！');
                }
            }

            Db::startTrans();
            try {
                $id = ZhiJintuiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    ZhiJintuiListModel::insert($vv);
                }
                (new FlowService())->save_bill('zhi_jintui', $id);

                (new OptService())->relation_file($files,'zhi_jintui',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_jintui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_jintui', $id, '进项退税');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_jintui_number();

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_jintui');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_jintui');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_jintui');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_jintui');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_jintui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_jintui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }






            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_jintui') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入本次退税进项票明细');
                }
            }


            //本次退税金额合计不可大于剩余可退进项税额！否则无法提交。
            if($param['isturn'] > 0) {
                $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                if($param['money'] > $engininfo['shengjintuifee']){
                    return outputJson(4000, '本次退税金额合计不可大于剩余可退进项税额！');
                }
            }


            Db::startTrans();
            try {
                ZhiJintuiModel::update($param);

                ZhiJintuiListModel::where(['zhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    ZhiJintuiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_jintui', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_jintui', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_jintui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_jintui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_jintui', $param['id'], '进项退税');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiJintuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = ZhiJintuiListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $piaoinfo = PiaoShouListModel::where(['id'=>$item['piaoid']])->find();
                $item['nums'] = $piaoinfo['nums'];
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($piaoinfo['piaotype']);
                $item['gonger'] = $piaoinfo['gonger'];
                $item['fee'] = $piaoinfo['fee'];
                $item['rate'] = $piaoinfo['rate'];
                $item['ratefee'] = $piaoinfo['ratefee'];
                $item['piaomoney'] = $piaoinfo['money'];
            }
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_jintui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_jintui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_jintui',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_jintui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_jintui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_lianjie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['iskou'])) {
                    return outputJson(4000, '请选择本次结算是否扣收');
                }
                if (empty($param['jiedt'])) {
                    return outputJson(4000, '请选择结算日期');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }


            $param['feilei'] = (new UserService())->get_optionfeiid_bynum('xmzc_qtf');
            $param['feitype'] = (new UserService())->get_optionfeiid_bynum('xmzc_qtf_lyjs');

            $param['number'] = (new BianhaoService())->create_zhi_lianjie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_lianjie') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            if(!isset($param['kou'])){
                $param['kou'] = [];
            }
            $kou = array_filter($param['kou']);
            unset($param['kou']);
            if($param['isturn'] > 0){
                if($param['iskou'] == '是')
                    if(count($kou) < 1){
                        return outputJson(4000, '请输入扣收明细');
                    }
            }


            Db::startTrans();
            try {
                $id = ZhiLianjieModel::insertGetId($param);


                foreach($kou as $vv){
                    $vv['zhiid'] = $id;
                    $vv['enginid'] = $param['enginid'];
                    ShouXiangKouModel::insert($vv);
                }

                (new FlowService())->save_bill('zhi_lianjie', $id);

                (new OptService())->relation_file($files,'zhi_lianjie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_lianjie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_lianjie', $id, '联营结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_lianjie_number();
            $data['kou_feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_lianjie');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_lianjie');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_lianjie');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_lianjie');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_lianjie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_lianjie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['iskou'])) {
                    return outputJson(4000, '请选择本次结算是否扣收');
                }
                if (empty($param['jiedt'])) {
                    return outputJson(4000, '请选择结算日期');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_lianjie') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            if(!isset($param['kou'])){
                $param['kou'] = [];
            }
            $kou = array_filter($param['kou']);
            unset($param['kou']);
            if($param['isturn'] > 0){
                if($param['iskou'] == '是')
                    if(count($kou) < 1){
                        return outputJson(4000, '请输入扣收明细');
                    }
            }


            Db::startTrans();
            try {
                ZhiLianjieModel::update($param);


                ShouXiangKouModel::where(['zhiid'=>$param['id']])->delete();
                foreach($kou as $vv){
                    $vv['zhiid'] = $param['id'];
                    $vv['enginid'] = $param['enginid'];
                    ShouXiangKouModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_lianjie', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_lianjie', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_lianjie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_lianjie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_lianjie', $param['id'], '联营结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiLianjieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $kou = ShouXiangKouModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($kou as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['kou'] = $kou;

            $data['kou_feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_lianjie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_lianjie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_lianjie',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_lianjie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_lianjie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_stuff_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['rutype'])){
                    return outputJson(4000, '请选择入库类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入入库主题');
                }
                if($param['rutype'] == 1){
                    if(empty($param['caiid'])){
                        return outputJson(4000, '请选择支出合同');
                    }
                    $param['buyid'] = 0;
                }elseif($param['rutype'] == 2){
                    if(empty($param['buyid'])){
                        return outputJson(4000, '请选择采购单');
                    }
                    $param['caiid'] = 0;
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
                if(empty($param['rudt'])){
                    return outputJson(4000, '请选择入库日期');
                }
                if($param['ischu'] > '是' && empty($param['baner_chu'])){
                    return outputJson(4000, '请输入领料人');
                }
            }


            $param['number'] = (new BianhaoService())->create_stuff_ru_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'stuff_ru') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入入库明细');
                }

                foreach ($list as $kk=>$vv){
                    if($param['rutype'] == 1){
                        $shengnum = ContractCaiListModel::where(['id'=>$vv['listid']])->value('shengnum');
                        if((int)$vv['num'] > $shengnum){
                            return outputJson(4000, '第'.$kk.'行剩余可入库数量为'.$shengnum);
                        }
                    }
                    if($param['rutype'] == 2){
                        $shengnum = ContractBuyListModel::where(['id'=>$vv['listid']])->value('shengnum');
                        if((int)$vv['num'] > $shengnum){
                            return outputJson(4000, '第'.$kk.'行剩余可入库数量为'.$shengnum);
                        }
                    }
                }
            }



            Db::startTrans();
            try {
                $id = StuffRuModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['ruid'] = $id;
                    StuffRuListModel::insert($vv);
                }

                (new FlowService())->save_bill('stuff_ru', $id);

                (new OptService())->relation_file($files,'stuff_ru',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'stuff_ru', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'stuff_ru', $id, '材料入库单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_stuff_ru_number();
            $data['rutype'] = (new StuffService())->get_stuff_ru_rutype();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('stuff_ru');
            $data['files'] = (new OptService())->get_file('stuff_ru');
            $data['isadvan'] = (new FlowService())->check_isadvan('stuff_ru');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('stuff_ru');
            $data['isnext'] = (new FlowService())->check_isnext('stuff_ru');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['rutype'])){
                    return outputJson(4000, '请选择入库类型');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入入库主题');
                }
                if($param['rutype'] == 1){
                    if(empty($param['caiid'])){
                        return outputJson(4000, '请选择支出合同');
                    }
                    $param['buyid'] = 0;
                }elseif($param['rutype'] == 2){
                    if(empty($param['buyid'])){
                        return outputJson(4000, '请选择采购单');
                    }
                    $param['caiid'] = 0;
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应商');
                }
                if(empty($param['rudt'])){
                    return outputJson(4000, '请选择入库日期');
                }
                if($param['ischu'] > '是' && empty($param['baner_chu'])){
                    return outputJson(4000, '请输入领料人');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'stuff_ru') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入入库明细');
                }


                foreach ($list as $kk=>$vv){
                    if($param['rutype'] == 1){
                        $shengnum = ContractCaiListModel::where(['id'=>$vv['listid']])->value('shengnum');
                        if((int)$vv['num'] > $shengnum){
                            return outputJson(4000, '第'.$kk.'行剩余可入库数量为'.$shengnum);
                        }
                    }
                    if($param['rutype'] == 2){
                        $shengnum = ContractBuyListModel::where(['id'=>$vv['listid']])->value('shengnum');
                        if((int)$vv['num'] > $shengnum){
                            return outputJson(4000, '第'.$kk.'行剩余可入库数量为'.$shengnum);
                        }
                    }
                }
            }


            Db::startTrans();
            try {
                StuffRuModel::update($param);

                StuffRuListModel::where(['ruid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['ruid'] = $param['id'];
                    StuffRuListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('stuff_ru', $param['id']);
                }else{
                    (new FlowService())->save_bill('stuff_ru', $param['id']);
                }

                (new OptService())->relation_file($files,'stuff_ru',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'stuff_ru', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'stuff_ru', $param['id'], '材料入库单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = StuffRuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            $data['buyidinfo'] = ContractBuyModel::where(['id'=>$data['data']['buyid']])->find();



            $list = StuffRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['rutype'] = (new StuffService())->get_stuff_ru_rutype();

            $data['files'] = (new OptService())->get_file('stuff_ru', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('stuff_ru');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('stuff_ru',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('stuff_ru',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('stuff_ru');

            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_device_jin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入进场主题');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
            }



            $param['number'] = (new BianhaoService())->create_device_jin_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'device_jin') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进场明细');
                }
            }


            Db::startTrans();
            try {
                $id = DeviceJinModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jinid'] = $id;
                    DeviceJinListModel::insert($vv);
                }

                (new FlowService())->save_bill('device_jin', $id);

                (new OptService())->relation_file($files,'device_jin',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_jin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_jin', $id, '进场登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_device_jin_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('device_jin');
            $data['files'] = (new OptService())->get_file('device_jin');
            $data['isadvan'] = (new FlowService())->check_isadvan('device_jin');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('device_jin');
            $data['isnext'] = (new FlowService())->check_isnext('device_jin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_jin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入进场主题');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'device_jin') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进场明细');
                }
            }

            Db::startTrans();
            try {
                DeviceJinModel::update($param);

                DeviceJinListModel::where(['jinid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jinid'] = $param['id'];
                    DeviceJinListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('device_jin', $param['id']);
                }else{
                    (new FlowService())->save_bill('device_jin', $param['id']);
                }

                (new OptService())->relation_file($files,'device_jin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_jin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_jin', $param['id'], '进场登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceJinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($data['data']['caiid']);

            $list = DeviceJinListModel::where(['jinid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_jin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('device_jin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('device_jin',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('device_jin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('device_jin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_device_yun(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入运行登记主题');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }



            $param['number'] = (new BianhaoService())->create_device_yun_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'device_yun') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入运行明细');
                }
            }


            Db::startTrans();
            try {
                $id = DeviceYunModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['yunid'] = $id;
                    DeviceYunListModel::insert($vv);
                }

                (new FlowService())->save_bill('device_yun', $id);

                (new OptService())->relation_file($files,'device_yun',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_yun', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_yun', $id, '设备运行登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_device_yun_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('device_yun');
            $data['files'] = (new OptService())->get_file('device_yun');
            $data['isadvan'] = (new FlowService())->check_isadvan('device_yun');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('device_yun');
            $data['isnext'] = (new FlowService())->check_isnext('device_yun');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_yun(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入运行登记主题');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'device_yun') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入运行明细');
                }
            }

            Db::startTrans();
            try {
                DeviceYunModel::update($param);

                DeviceYunListModel::where(['yunid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['yunid'] = $param['id'];
                    DeviceYunListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('device_yun', $param['id']);
                }else{
                    (new FlowService())->save_bill('device_yun', $param['id']);
                }

                (new OptService())->relation_file($files,'device_yun',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_yun', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_yun', $param['id'], '设备运行登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceYunModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = DeviceYunListModel::alias('list')
                ->field('list.*,ming.name,ming.unit,ming.price,ming.shenum,ming.jindt
                ,cai.name as cainame,cai.gonger')
                ->leftJoin('device_jin_list ming','ming.id=list.listid')
                ->leftJoin('device_jin jin','jin.id=ming.jinid')
                ->leftJoin('contract_cai cai','cai.id=jin.caiid')
                ->where(['list.yunid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_yun', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('device_yun');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('device_yun',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('device_yun',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('device_yun');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_device_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入退场登记主题');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }



            $param['number'] = (new BianhaoService())->create_device_tui_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'device_tui') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入退场明细');
                }
            }


            Db::startTrans();
            try {
                $id = DeviceTuiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['tuiid'] = $id;
                    DeviceTuiListModel::insert($vv);
                }

                (new FlowService())->save_bill('device_tui', $id);

                (new OptService())->relation_file($files,'device_tui',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'device_tui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'device_tui', $id, '设备退场登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_device_tui_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('device_tui');
            $data['files'] = (new OptService())->get_file('device_tui');
            $data['isadvan'] = (new FlowService())->check_isadvan('device_tui');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('device_tui');
            $data['isnext'] = (new FlowService())->check_isnext('device_tui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_device_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入退场登记主题');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'device_tui') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入退场明细');
                }
            }

            Db::startTrans();
            try {
                DeviceTuiModel::update($param);

                DeviceTuiListModel::where(['tuiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['tuiid'] = $param['id'];
                    DeviceTuiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('device_tui', $param['id']);
                }else{
                    (new FlowService())->save_bill('device_tui', $param['id']);
                }

                (new OptService())->relation_file($files,'device_tui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'device_tui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'device_tui', $param['id'], '设备退场登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DeviceTuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = DeviceTuiListModel::alias('list')
                ->field('list.*,ming.name,ming.unit,ming.price,ming.shenum,ming.jindt
                ,cai.name as cainame,cai.gonger')
                ->leftJoin('device_jin_list ming','ming.id=list.listid')
                ->leftJoin('device_jin jin','jin.id=ming.jinid')
                ->leftJoin('contract_cai cai','cai.id=jin.caiid')
                ->where(['list.tuiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('device_tui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('device_tui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('device_tui',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('device_tui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('device_tui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_labour_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'gongtype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'gonglei') !== false){
                    unset($param[$k]);
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'labour_xiang') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入花名册明细');
                }
            }



            Db::startTrans();
            try {
                $id = LabourXiangModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['xiangid'] = $id;
                    LabourXiangListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('labour_xiang', $id);

                (new OptService())->relation_file($files,'labour_xiang',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_xiang', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_xiang', $id, '项目花名册');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['gonglei'] = (new OptService())->get_base_info('engin_labour_gonglei');


            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('labour_xiang');
            $data['files'] = (new OptService())->get_file('labour_xiang');
            $data['isadvan'] = (new FlowService())->check_isadvan('labour_xiang');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('labour_xiang');
            $data['isnext'] = (new FlowService())->check_isnext('labour_xiang');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_xiang(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'gongtype') !== false){
                    unset($param[$k]);
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'gonglei') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'labour_xiang') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入花名册明细');
                }
            }

            Db::startTrans();
            try {
                LabourXiangModel::update($param);

                LabourXiangListModel::where(['xiangid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['xiangid'] = $param['id'];
                    LabourXiangListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('labour_xiang', $param['id']);
                }else{
                    (new FlowService())->save_bill('labour_xiang', $param['id']);
                }

                (new OptService())->relation_file($files,'labour_xiang',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_xiang', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_xiang', $param['id'], '项目花名册');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourXiangModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = LabourXiangListModel::where(['xiangid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['gongtype_name'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
                $item['gonglei_name'] = (new OptService())->get_base_showname('engin_labour_gonglei', $item['gonglei']);
            }
            $data['list'] = $list;

            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['gonglei'] = (new OptService())->get_base_info('engin_labour_gonglei');
            $data['files'] = (new OptService())->get_file('labour_xiang', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('labour_xiang');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('labour_xiang',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('labour_xiang',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('labour_xiang');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_labour_yong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入用工主题');
                }
                if(empty($param['yongxing'])){
                    return outputJson(4000, '请选择用工性质');
                }
                if($param['yongxing'] == '劳务分包'){
                    if(empty($param['caiid'])){
                        return outputJson(4000, '请选择关联合同');
                    }
                    $param['gonger'] = ContractCaiModel::where(['id'=>$param['caiid']])->value('gonger');
                }else{
                    $param['caiid'] = 0;
                    $param['gonger'] = '';
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }
            if(is_array($param['yongdt'])){
                $param['yongdt'] = implode(' - ', $param['yongdt']);
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'gongtype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'gonglei') !== false){
                    unset($param[$k]);
                }
            }

            $param['number'] = (new BianhaoService())->create_labour_yong_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'labour_yong') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入用工明细');
                }
            }



            Db::startTrans();
            try {
                $id = LabourYongModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['yongid'] = $id;
                    LabourYongListModel::insertGetId($vv);
                }

                (new FlowService())->save_bill('labour_yong', $id);

                (new OptService())->relation_file($files,'labour_yong',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_yong', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_yong', $id, '用工单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['gonglei'] = (new OptService())->get_base_info('engin_labour_gonglei');


            $data['number'] = (new BianhaoService())->create_labour_yong_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('labour_yong');
            $data['files'] = (new OptService())->get_file('labour_yong');
            $data['isadvan'] = (new FlowService())->check_isadvan('labour_yong');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('labour_yong');
            $data['isnext'] = (new FlowService())->check_isnext('labour_yong');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_yong(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入用工主题');
                }
                if(empty($param['yongxing'])){
                    return outputJson(4000, '请选择用工性质');
                }
                if($param['yongxing'] == '劳务分包'){
                    if(empty($param['caiid'])){
                        return outputJson(4000, '请选择关联合同');
                    }
                    $param['gonger'] = ContractCaiModel::where(['id'=>$param['caiid']])->value('gonger');
                }else{
                    $param['caiid'] = 0;
                    $param['gonger'] = '';
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入填报人');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择填报日期');
                }
            }


            if(is_array($param['yongdt'])){
                $param['yongdt'] = implode(' - ', $param['yongdt']);
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'gongtype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'gonglei') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'labour_yong') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入用工明细');
                }
            }

            Db::startTrans();
            try {
                LabourYongModel::update($param);

                LabourYongListModel::where(['yongid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['yongid'] = $param['id'];
                    LabourYongListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('labour_yong', $param['id']);
                }else{
                    (new FlowService())->save_bill('labour_yong', $param['id']);
                }

                (new OptService())->relation_file($files,'labour_yong',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_yong', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_yong', $param['id'], '用工单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourYongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['yongdtarr'] = explode(' - ',$data['data']['yongdt']);

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $caiidinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiidinfo'] = $caiidinfo;

            $list = LabourYongListModel::where(['yongid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['gongtype_name'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
                $item['gonglei_name'] = (new OptService())->get_base_showname('engin_labour_gonglei', $item['gonglei']);
            }
            $data['list'] = $list;

            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['gonglei'] = (new OptService())->get_base_info('engin_labour_gonglei');
            $data['files'] = (new OptService())->get_file('labour_yong', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('labour_yong');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('labour_yong',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('labour_yong',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('labour_yong');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_diary(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择日期');
                }
                if(empty($param['tian'])){
                    return outputJson(4000, '请输入天气情况');
                }

                if((new SceneService())->check_engin_diary_dt($param['dt'], $param['enginid'])){
                    return outputJson(4000, '该项目日志日期已存在');
                }
            }


            $param['tianer'] = $this->userinfo['realname'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_diary') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginDiaryModel::insertGetId($param);

                (new FlowService())->save_bill('engin_diary', $id);

                (new OptService())->relation_file($files,'engin_diary',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_diary', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_diary', $id, '施工日志');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['tianer'] = $this->userinfo['realname'];
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_diary');
            $data['files'] = (new OptService())->get_file('engin_diary');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_diary');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_diary');
            $data['isnext'] = (new FlowService())->check_isnext('engin_diary');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_diary(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择日期');
                }
                if(empty($param['tian'])){
                    return outputJson(4000, '请输入天气情况');
                }

                if((new SceneService())->check_engin_diary_dt($param['dt'], $param['enginid'], $param['id'])){
                    return outputJson(4000, '该项目日志日期已存在');
                }
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_diary') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginDiaryModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_diary', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_diary', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_diary',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_diary', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_diary', $param['id'], '施工日志');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginDiaryModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $data['files'] = (new OptService())->get_file('engin_diary', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_diary');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_diary',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_diary',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_diary');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_speed(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入填报名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择填报月份');
                }

                if((new SceneService())->check_engin_speed_month($param['month'], $param['enginid'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }

            }



            $param['number'] = (new BianhaoService())->create_engin_speed_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_speed') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入填报明细');
                }
            }


            Db::startTrans();
            try {
                $id = EnginSpeedModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['speedid'] = $id;
                    EnginSpeedListModel::where(['zongid'=>$vv['zongid']])->update(['findt'=>$vv['findt']]);
                    EnginSpeedListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_speed', $id);
                (new OptService())->relation_file($files,'engin_speed',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_speed', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_speed', $id, '进度填报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_engin_speed_number();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_speed');
            $data['files'] = (new OptService())->get_file('engin_speed');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_speed');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_speed');
            $data['isnext'] = (new FlowService())->check_isnext('engin_speed');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_speed(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入填报名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择填报月份');
                }

                if((new SceneService())->check_engin_speed_month($param['month'], $param['enginid'], $param['id'])){
                    return outputJson(4000, '该项目填报月份已存在');
                }

            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_speed') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入填报明细');
                }

            }

            Db::startTrans();
            try {
                EnginSpeedModel::update($param);

                EnginSpeedListModel::where(['speedid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['speedid'] = $param['id'];
                    EnginSpeedListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_speed', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_speed', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_speed',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_speed', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_speed', $param['id'], '进度填报');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginSpeedModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['monthinfo'] = SpeedMonthModel::get_info_by_pk($data['data']['monthid']);

            $list = EnginSpeedListModel::where(['speedid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('engin_speed', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_speed');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_speed',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_speed',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_speed');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_piao_tax(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择预缴月份');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择发生日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'ratetype') !== false){
                    unset($param[$k]);
                }
            }

            $param['number'] = (new BianhaoService())->create_piao_tax_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'piao_tax') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预缴明细');
                }
            }


            Db::startTrans();
            try {
                $id = PiaoTaxModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['taxid'] = $id;
                        PiaoTaxListModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('piao_tax', $id);

                (new OptService())->relation_file($files,'piao_tax',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_tax', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_tax', $id, '税费预缴');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_piao_tax_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['ratetype'] = (new OptService())->get_base_info('cai_ratetype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('piao_tax');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_tax');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('piao_tax');
            $data['isnext'] = (new FlowService())->check_isnext('piao_tax');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['files'] = (new OptService())->get_file('piao_tax');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_tax(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['month'])){
                    return outputJson(4000, '请选择预缴月份');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择发生日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'ratetype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'piao_tax') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入预缴明细');
                }
            }


            Db::startTrans();
            try {
                PiaoTaxModel::update($param);

                PiaoTaxListModel::where(['taxid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['taxid'] = $param['id'];
                        PiaoTaxListModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_tax', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_tax', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_tax',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_tax', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_tax', $param['id'], '税费预缴');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoTaxModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = PiaoTaxListModel::where(['taxid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['ratetype_name'] = (new OptService())->get_base_showname('cai_ratetype', $item['ratetype']);
            }
            $data['list'] = $list;


            $data['ratetype'] = (new OptService())->get_base_info('cai_ratetype');
            $data['files'] = (new OptService())->get_file('piao_tax', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_tax');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_tax',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('piao_tax',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_tax');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['feitype'])) {
                    return outputJson(4000, '请选择费用项');
                }else{
                    $feitypeid = (new UserService())->get_optionfeiid_bynum('wlk_byj_xmbyj');
                    if($param['feitype'] == $feitypeid){
                        if (empty($param['enginid'])) {
                            return outputJson(4000, '请选择项目名称');
                        }
                    }
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }
            $param['feilei'] = (new UserService())->get_optionfeiid_bynum('wlk_byj');

            $param['number'] = (new BianhaoService())->create_zhi_jie_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_jie') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ZhiJieModel::insertGetId($param);


                (new FlowService())->save_bill('zhi_jie', $id);

                (new OptService())->relation_file($files,'zhi_jie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_jie', $id, '备用金借支');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_jie_number();
            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_byj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shanginfo'] = ZhiJieModel::where(['created_user'=>$this->userinfo['id'],'deleted_user'=>0,'isturn'=>1])->where('status','in',[0,1])->find();

            $data['files'] = (new OptService())->get_file('zhi_jie');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_jie');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_jie');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['feitype'])) {
                    return outputJson(4000, '请选择费用项');
                }else{
                    $feitypeid = (new UserService())->get_optionfeiid_bynum('wlk_byj_xmbyj');
                    if($param['feitype'] == $feitypeid){
                        if (empty($param['enginid'])) {
                            return outputJson(4000, '请选择项目名称');
                        }
                    }
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_jie') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);



            Db::startTrans();
            try {
                ZhiJieModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_jie', $param['id'], '备用金借支');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_byj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_jie',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_shou_huan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['jieid'])) {
                    return outputJson(4000, '请选择关联备用金');
                }else{
                    $zhijieinfo = ZhiJieModel::where(['id'=>$param['jieid']])->find();
                    $param['feitype'] = $zhijieinfo['feitype'];
                    if($param['money'] > $zhijieinfo['yufee']){
                        return outputJson(4000, '还款金额大于备用金余额，请修改后提交');
                    }
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入还款金额');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
            }

            $param['duier'] = $this->userinfo['realname'];
            $param['feilei'] = (new UserService())->get_optionfeiid_bynum('wlk_byj');

            $param['number'] = (new BianhaoService())->create_shou_huan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'shou_huan') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ShouHuanModel::insertGetId($param);

                (new FlowService())->save_bill('shou_huan', $id);

                (new OptService())->relation_file($files,'shou_huan',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'shou_huan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'shou_huan', $id, '备用金还款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_shou_huan_number();
            $data['dt'] = date("Y-m-d");
            $data['duier'] = $this->userinfo['realname'];

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_byj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('shou_huan');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('shou_huan');
            $data['isadvan'] = (new FlowService())->check_isadvan('shou_huan');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('shou_huan');
            $data['isnext'] = (new FlowService())->check_isnext('shou_huan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_shou_huan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['jieid'])) {
                    return outputJson(4000, '请选择关联备用金');
                }else{
                    $zhijieinfo = ZhiJieModel::where(['id'=>$param['jieid']])->find();
                    $param['feitype'] = $zhijieinfo['feitype'];
                    if($param['money'] > $zhijieinfo['yufee']){
                        return outputJson(4000, '还款金额大于备用金余额，请修改后提交');
                    }
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入还款金额');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'shou_huan') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ShouHuanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('shou_huan', $param['id']);
                }else{
                    (new FlowService())->save_bill('shou_huan', $param['id']);
                }

                (new OptService())->relation_file($files,'shou_huan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'shou_huan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'shou_huan', $param['id'], '备用金还款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ShouHuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['jieidinfo'] = ZhiJieModel::where(['id'=>$data['data']['jieid']])->find();
            if($data['jieidinfo']){
                $data['jieidinfo']['feitype_name'] = OptionFeiModel::where(['id'=>$data['jieidinfo']['feitype']])->value('name');
            }
            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('shou_huan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('shou_huan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('shou_huan',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('shou_huan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('shou_huan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_daybao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
                if ($param['isdi'] == '是') {
                    if (empty($param['jieid'])) {
                        return outputJson(4000, '请选择关联备用金');
                    }
                }else{
                    $param['jieid'] = 0;
                    $param['jieyufee'] = 0;
                    $param['difee'] = 0;
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['qtzc_qtzc']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_zhi_daybao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_daybao') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }

            Db::startTrans();
            try {
                $id = ZhiDaybaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    ZhiDaybaoListModel::insert($vv);
                }
                (new FlowService())->save_bill('zhi_daybao', $id);

                (new OptService())->relation_file($files,'zhi_daybao',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_daybao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_daybao', $id, '日常费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_daybao_number();
            $data['myjieyufee'] = ZhiJieModel::where(['enginid'=>0,'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$this->userinfo['id']])->sum('yufee');

            $data['shanginfo'] = ZhiDaybaoModel::where(['created_user'=>$this->userinfo['id'],'deleted_user'=>0,'isturn'=>1])->where('status','in',[0,1])->find();

            $data['feitype'] = (new UserService())->get_option_fei_slet(['qtzc_qtzc']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_daybao');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_daybao');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_daybao');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_daybao');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_daybao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_daybao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
                if ($param['isdi'] == '是') {
                    if (empty($param['jieid'])) {
                        return outputJson(4000, '请选择关联备用金');
                    }
                }else{
                    $param['jieid'] = 0;
                    $param['jieyufee'] = 0;
                    $param['difee'] = 0;
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_daybao') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
            }


            Db::startTrans();
            try {
                ZhiDaybaoModel::update($param);

                ZhiDaybaoListModel::where(['zhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    ZhiDaybaoListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_daybao', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_daybao', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_daybao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_daybao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_daybao', $param['id'], '日常费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiDaybaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['myjieyufee'] = ZhiJieModel::where(['enginid'=>0,'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$this->userinfo['id']])->sum('yufee');
            $data['jieidinfo'] = ZhiJieModel::where(['id'=>$data['data']['jieid']])->find();

            $list = ZhiDaybaoListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['qtzc_qtzc']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_daybao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_daybao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_daybao',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_daybao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_daybao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_zhi_dayfu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {

                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['qtzc_qtzc']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_zhi_dayfu_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_dayfu') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                $id = ZhiDayfuModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    ZhiDayfuListModel::insert($vv);
                }
                (new FlowService())->save_bill('zhi_dayfu', $id);

                (new OptService())->relation_file($files,'zhi_dayfu',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'zhi_dayfu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'zhi_dayfu', $id, '日常付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_zhi_dayfu_number();

            $data['feitype'] = (new UserService())->get_option_fei_slet(['qtzc_qtzc']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('zhi_dayfu');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('zhi_dayfu');
            $data['isadvan'] = (new FlowService())->check_isadvan('zhi_dayfu');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('zhi_dayfu');
            $data['isnext'] = (new FlowService())->check_isnext('zhi_dayfu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_zhi_dayfu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'zhi_dayfu') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                ZhiDayfuModel::update($param);

                ZhiDayfuListModel::where(['zhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    ZhiDayfuListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('zhi_dayfu', $param['id']);
                }else{
                    (new FlowService())->save_bill('zhi_dayfu', $param['id']);
                }

                (new OptService())->relation_file($files,'zhi_dayfu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'zhi_dayfu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'zhi_dayfu', $param['id'], '日常付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ZhiDayfuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $list = ZhiDayfuListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['qtzc_qtzc']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('zhi_dayfu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('zhi_dayfu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('zhi_dayfu',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('zhi_dayfu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('zhi_dayfu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_shou_qi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['qtsr_qtsr']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_shou_qi_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'shou_qi') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                $id = ShouQiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shouid'] = $id;
                    ShouQiListModel::insert($vv);
                }
                (new FlowService())->save_bill('shou_qi', $id);

                (new OptService())->relation_file($files,'shou_qi',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'shou_qi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'shou_qi', $id, '其他收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_shou_qi_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['feitype'] = (new UserService())->get_option_fei_slet(['qtsr_qtsr']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('shou_qi');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('shou_qi');
            $data['isadvan'] = (new FlowService())->check_isadvan('shou_qi');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('shou_qi');
            $data['isnext'] = (new FlowService())->check_isnext('shou_qi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_shou_qi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'shou_qi') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                ShouQiModel::update($param);

                ShouQiListModel::where(['shouid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shouid'] = $param['id'];
                    ShouQiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('shou_qi', $param['id']);
                }else{
                    (new FlowService())->save_bill('shou_qi', $param['id']);
                }

                (new OptService())->relation_file($files,'shou_qi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'shou_qi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'shou_qi', $param['id'], '其他收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ShouQiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $list = ShouQiListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
            }
            $data['list'] = $list;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['qtsr_qtsr']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('shou_qi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('shou_qi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('shou_qi',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('shou_qi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('shou_qi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['helei'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择采购方(甲方)');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }

            $param['number'] = (new BianhaoService())->create_contract_day_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_day') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }



            Db::startTrans();
            try {
                $id = ContractDayModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['dayid'] = $id;
                    ContractDayListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_day', $id);

                (new OptService())->relation_file($files,'contract_day',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_day', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_day', $id, '日常支出合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_contract_day_number();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['helei'] = (new OptService())->get_base_info('cai_contract_day_helei');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('contract_day');
            $data['files'] = (new OptService())->get_file('contract_day');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_day');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('contract_day');
            $data['isnext'] = (new FlowService())->check_isnext('contract_day');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['helei'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['year'])){
                    return outputJson(4000, '请选择合同年份');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择采购方(甲方)');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if($param['rate'] == ''){
                    return outputJson(4000, '请输入税率');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择合同状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'contract_day') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }

            Db::startTrans();
            try {
                ContractDayModel::update($param);


                ContractDayListModel::where(['dayid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dayid'] = $param['id'];
                    ContractDayListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_day', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_day', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_day',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_day', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_day', $param['id'], '日常支出合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractDayModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');

            $list = ContractDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['helei'] = (new OptService())->get_base_info('cai_contract_day_helei');

            $data['files'] = (new OptService())->get_file('contract_day', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_day');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_day',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('contract_day',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_day');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bond_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['feitype'])) {
                    return outputJson(4000, '请选择费用项');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入到账金额');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_bond_shou_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_shou') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = BondShouModel::insertGetId($param);

                (new FlowService())->save_bill('bond_shou', $id);

                (new OptService())->relation_file($files,'bond_shou',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bond_shou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bond_shou', $id, '保证金收取');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_bond_shou_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('bond_shou');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bond_shou');
            $data['isadvan'] = (new FlowService())->check_isadvan('bond_shou');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('bond_shou');
            $data['isnext'] = (new FlowService())->check_isnext('bond_shou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bond_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['feitype'])) {
                    return outputJson(4000, '请选择费用项');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入到账金额');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_shou') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                BondShouModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bond_shou', $param['id']);
                }else{
                    (new FlowService())->save_bill('bond_shou', $param['id']);
                }

                (new OptService())->relation_file($files,'bond_shou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bond_shou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bond_shou', $param['id'], '保证金收取');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BondShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('bond_shou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bond_shou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bond_shou',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('bond_shou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bond_shou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bond_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }else{
                    $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                    if($engininfo['business'] == 2){
                        if (empty($param['isshou'])) {
                            return outputJson(4000, '请选择是否预收取');
                        }else{
                            if ($param['isshou'] == '是') {
                                if (empty($param['shouid'])) {
                                    return outputJson(4000, '请选择关联保证金收取');
                                }
                            }
                        }
                    }
                }
                if (empty($param['feitype'])) {
                    return outputJson(4000, '请选择费用项');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入申请缴纳金额');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }

            $feileiInfo = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $param['feilei'] = $feileiInfo[0]['id'];

            $param['number'] = (new BianhaoService())->create_bond_jiao_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_jiao') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = BondJiaoModel::insertGetId($param);

                (new FlowService())->save_bill('bond_jiao', $id);

                (new OptService())->relation_file($files,'bond_jiao',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bond_jiao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bond_jiao', $id, '保证金缴纳');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_bond_jiao_number();

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('bond_jiao');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bond_jiao');
            $data['isadvan'] = (new FlowService())->check_isadvan('bond_jiao');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('bond_jiao');
            $data['isnext'] = (new FlowService())->check_isnext('bond_jiao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bond_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }else{
                    $engininfo = EnginModel::where(['id'=>$param['enginid']])->find();
                    if($engininfo['business'] == 2){
                        if (empty($param['isshou'])) {
                            return outputJson(4000, '请选择是否预收取');
                        }else{
                            if ($param['isshou'] == '是') {
                                if (empty($param['shouid'])) {
                                    return outputJson(4000, '请选择关联保证金收取');
                                }
                            }
                        }
                    }
                }
                if (empty($param['feitype'])) {
                    return outputJson(4000, '请选择费用项');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入申请缴纳金额');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_jiao') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                BondJiaoModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bond_jiao', $param['id']);
                }else{
                    (new FlowService())->save_bill('bond_jiao', $param['id']);
                }

                (new OptService())->relation_file($files,'bond_jiao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bond_jiao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bond_jiao', $param['id'], '保证金缴纳');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BondJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['shouidinfo'] = BondShouModel::where(['id'=>$data['data']['shouid']])->find();

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');


            $data['files'] = (new OptService())->get_file('bond_jiao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bond_jiao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bond_jiao',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('bond_jiao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bond_jiao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bond_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['jiaoid'])) {
                    return outputJson(4000, '请选择关联保证金缴纳');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入退回到账金额');
                }
                if ($param['koufee'] == '') {
                    return outputJson(4000, '请输入扣款金额');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }

            if (!empty($param['jiaoid'])) {
                $jiaoinfo = BondJiaoModel::where(['id'=>$param['jiaoid']])->find();
                $param['feilei'] = $jiaoinfo['feilei'];
                $param['feitype'] = $jiaoinfo['feitype'];
            }

            $param['number'] = (new BianhaoService())->create_bond_tui_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_tui') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);


            Db::startTrans();
            try {
                $id = BondTuiModel::insertGetId($param);

                (new FlowService())->save_bill('bond_tui', $id);

                (new OptService())->relation_file($files,'bond_tui',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bond_tui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bond_tui', $id, '保证金退回');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_bond_tui_number();
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('bond_tui');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bond_tui');
            $data['isadvan'] = (new FlowService())->check_isadvan('bond_tui');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('bond_tui');
            $data['isnext'] = (new FlowService())->check_isnext('bond_tui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bond_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['jiaoid'])) {
                    return outputJson(4000, '请选择关联保证金缴纳');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入退回到账金额');
                }
                if ($param['koufee'] == '') {
                    return outputJson(4000, '请输入扣款金额');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择付款方');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择到账方式');
                }
                if (empty($param['accid'])) {
                    return outputJson(4000, '请选择到账账户');
                }
                if (empty($param['dt'])) {
                    return outputJson(4000, '请选择到账日期');
                }
                if (empty($param['baner'])) {
                    return outputJson(4000, '请输入经办人');
                }
            }


            if (!empty($param['jiaoid'])) {
                $jiaoinfo = BondJiaoModel::where(['id'=>$param['jiaoid']])->find();
                $param['feilei'] = $jiaoinfo['feilei'];
                $param['feitype'] = $jiaoinfo['feitype'];
            }



            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_tui') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                BondTuiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bond_tui', $param['id']);
                }else{
                    (new FlowService())->save_bill('bond_tui', $param['id']);
                }

                (new OptService())->relation_file($files,'bond_tui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bond_tui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bond_tui', $param['id'], '保证金退回');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BondTuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['jiaoidinfo'] = BondJiaoModel::where(['id'=>$data['data']['jiaoid']])->find();
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $data['data']['feitype_name'] = OptionFeiModel::where(['id'=>$data['data']['feitype']])->value('name');
            $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');

            $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_bzj']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('bond_tui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bond_tui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bond_tui',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('bond_tui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bond_tui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bond_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['shouid'])) {
                    return outputJson(4000, '请选择关联保证金收取');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入申请返款金额');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }

                $shouinfo = BondShouModel::where(['id'=>$param['shouid']])->find();
                if($param['money'] > $shouinfo['kefanfee']){
                    return outputJson(4000, '申请返款金额已超过可返款金额，请重新提交');
                }
            }

            if (!empty($param['shouid'])) {
                $shouinfo = BondShouModel::where(['id'=>$param['shouid']])->find();
                $param['feilei'] = $shouinfo['feilei'];
                $param['feitype'] = $shouinfo['feitype'];
            }

            $param['number'] = (new BianhaoService())->create_bond_fan_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_fan') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = BondFanModel::insertGetId($param);

                (new FlowService())->save_bill('bond_fan', $id);

                (new OptService())->relation_file($files,'bond_fan',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bond_fan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bond_fan', $id, '保证金返还');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_bond_fan_number();

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('bond_fan');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bond_fan');
            $data['isadvan'] = (new FlowService())->check_isadvan('bond_fan');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('bond_fan');
            $data['isnext'] = (new FlowService())->check_isnext('bond_fan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bond_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['shouid'])) {
                    return outputJson(4000, '请选择关联保证金收取');
                }
                if (empty($param['zhai'])) {
                    return outputJson(4000, '请输入款项摘要');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入申请返款金额');
                }
                if (empty($param['incdaotype'])) {
                    return outputJson(4000, '请选择支付方式');
                }
                if (empty($param['duier'])) {
                    return outputJson(4000, '请选择收款方');
                }
                if (empty($param['banknum'])) {
                    return outputJson(4000, '请选择账户账号');
                }

                $shouinfo = BondShouModel::where(['id'=>$param['shouid']])->find();
                if($param['money'] > $shouinfo['kefanfee']){
                    return outputJson(4000, '申请返款金额已超过可返款金额，请重新提交');
                }
            }

            if (!empty($param['shouid'])) {
                $shouinfo = BondShouModel::where(['id'=>$param['shouid']])->find();
                $param['feilei'] = $shouinfo['feilei'];
                $param['feitype'] = $shouinfo['feitype'];
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_fan') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                BondFanModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bond_fan', $param['id']);
                }else{
                    (new FlowService())->save_bill('bond_fan', $param['id']);
                }

                (new OptService())->relation_file($files,'bond_fan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bond_fan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bond_fan', $param['id'], '保证金返还');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BondFanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['shouidinfo'] = BondShouModel::where(['id'=>$data['data']['shouid']])->find();
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['data']['feitype_name'] = OptionFeiModel::where(['id'=>$data['data']['feitype']])->value('name');
            $data['data']['feilei_name'] = OptionFeiModel::where(['id'=>$data['data']['feilei']])->value('name');

            $data['files'] = (new OptService())->get_file('bond_fan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bond_fan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bond_fan',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('bond_fan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bond_fan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['hantype'])) {
                    return outputJson(4000, '请选择保函类别');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入保函金额');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_kai') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = BondKaiModel::insertGetId($param);

                (new FlowService())->save_bill('bond_kai', $id);

                (new OptService())->relation_file($files,'bond_kai',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bond_kai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bond_kai', $id, '项目保函开具');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');

            $data['files'] = (new OptService())->get_file('bond_kai');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('bond_kai');
            $data['isadvan'] = (new FlowService())->check_isadvan('bond_kai');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('bond_kai');
            $data['isnext'] = (new FlowService())->check_isnext('bond_kai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bond_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['enginid'])) {
                    return outputJson(4000, '请选择项目名称');
                }
                if (empty($param['hantype'])) {
                    return outputJson(4000, '请选择保函类别');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入保函金额');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'bond_kai') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                BondKaiModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bond_kai', $param['id']);
                }else{
                    (new FlowService())->save_bill('bond_kai', $param['id']);
                }

                (new OptService())->relation_file($files,'bond_kai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bond_kai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bond_kai', $param['id'], '项目保函开具');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BondKaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['hantype'] = (new OptService())->get_base_info('engin_bond_hantype');

            $data['files'] = (new OptService())->get_file('bond_kai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bond_kai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bond_kai',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('bond_kai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bond_kai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_wage(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }

            }



            $param['number'] = (new BianhaoService())->create_wage_number($param['month']);
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'wage') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }

            foreach ($list as &$mmm){
                $mmm['name'] = trim($mmm['name']);
            }



            Db::startTrans();
            try {
                $id = WageModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['wageid'] = $id;
                    WageListModel::insert($vv);
                }

                (new FlowService())->save_bill('wage', $id);

                (new OptService())->relation_file($files,'wage',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wage', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wage', $id, '工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wage');
            $data['files'] = (new OptService())->get_file('wage');
            $data['isadvan'] = (new FlowService())->check_isadvan('wage');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('wage');
            $data['isnext'] = (new FlowService())->check_isnext('wage');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wage(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }

            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'wage') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }

            foreach ($list as &$mmm){
                $mmm['name'] = trim($mmm['name']);
            }

            Db::startTrans();
            try {
                WageModel::update($param);

                WageListModel::where(['wageid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['wageid'] = $param['id'];
                    WageListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wage', $param['id']);
                }else{
                    (new FlowService())->save_bill('wage', $param['id']);
                }

                (new OptService())->relation_file($files,'wage',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wage', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wage', $param['id'], '工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WageModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WageListModel::where(['wageid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wage', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wage');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wage',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('wage',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wage');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_down(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_down') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            $param['listnum'] = count($list);

            Db::startTrans();
            try {
                $id = CertDownModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['mid'] = $id;
                    CertDownListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'cert_down',$id);
                (new FlowService())->save_bill('cert_down', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_down', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_down', $id, '证件下载申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_down');
            $data['files'] = (new OptService())->get_file('cert_down');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_down');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('cert_down');
            $data['isnext'] = (new FlowService())->check_isnext('cert_down');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_down(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_down') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            $param['listnum'] = count($list);

            Db::startTrans();
            try {
                CertDownModel::update($param);

                CertDownListModel::where(['mid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['mid'] = $param['id'];
                    CertDownListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'cert_down',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_down', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_down', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_down', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_down', $param['id'], '证件下载申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertDownModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertDownListModel::where(['mid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($item['type'] == 1){
                    $certinfo = CertModel::where(['id'=>$item['certid']])->find();
                    $person = PersonModel::where(['id'=>$certinfo['personid']])->find();
                    $item['major'] = str_replace('[]','，',$certinfo['major']);
                    $item['number'] = $certinfo['number'];
                    $item['form'] = $certinfo['form'];
                    $item['goes'] = $certinfo['goes'];
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$person['name'];
                }
                if($item['type'] == 2){
                    $certinfo = CertCompanyModel::where(['id'=>$item['certid']])->find();
                    $item['major'] = '';
                    $item['number'] = $certinfo['number'];
                    $item['form'] = $certinfo['form'];
                    $item['goes'] = $certinfo['goes'];
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$item['chanid_name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('cert_down', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_down');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_down',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('cert_down',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_down');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_borrow(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['lingtype'])){
                    return outputJson(4000, '请选择领取方式');
                }

            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_borrow') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != '在册'){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != '纸质原件'){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>1,'list.certid'=>$vvv['certid']])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != '在册'){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != '纸质原件'){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>2,'list.certid'=>$vvv['certid']])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            $param['listnum'] = count($list);

            Db::startTrans();
            try {
                $id = CertBorrowModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['mid'] = $id;
                    CertBorrowListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'cert_borrow',$id);
                (new FlowService())->save_bill('cert_borrow', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_borrow', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_borrow', $id, '借证申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['lingtype'] = (new CertService())->get_cert_borrow_lingtype();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_borrow');
            $data['files'] = (new OptService())->get_file('cert_borrow');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_borrow');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('cert_borrow');
            $data['isnext'] = (new FlowService())->check_isnext('cert_borrow');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_borrow(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['lingtype'])){
                    return outputJson(4000, '请选择领取方式');
                }

            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_borrow') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != '在册'){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != '纸质原件'){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }

                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>1,'list.certid'=>$vvv['certid']])
                            ->where('bw.id','neq',$param['id'])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != '正常'){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != '在册'){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != '纸质原件'){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>2,'list.certid'=>$vvv['certid']])
                            ->where('bw.id','neq',$param['id'])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            $param['listnum'] = count($list);


            Db::startTrans();
            try {
                CertBorrowModel::update($param);

                CertBorrowListModel::where(['mid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['mid'] = $param['id'];
                    CertBorrowListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'cert_borrow',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_borrow', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_borrow', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_borrow', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_borrow', $param['id'], '借证申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertBorrowModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertBorrowListModel::where(['mid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($item['type'] == 1){
                    $certinfo = CertModel::where(['id'=>$item['certid']])->find();
                    $person = PersonModel::where(['id'=>$certinfo['personid']])->find();
                    $item['major'] = str_replace('[]','，',$certinfo['major']);
                    $item['major_name'] = str_replace('[]','，',$certinfo['major']);
                    $item['number'] = $certinfo['number'];
                    $item['form'] = $certinfo['form'];
                    $item['goes'] = $certinfo['goes'];
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$person['name'];
                }
                if($item['type'] == 2){
                    $certinfo = CertCompanyModel::where(['id'=>$item['certid']])->find();
                    $item['major'] = '';
                    $item['major_name'] = '';
                    $item['number'] = $certinfo['number'];
                    $item['form'] = $certinfo['form'];
                    $item['goes'] = $certinfo['goes'];
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'];
                    if($certinfo['pid']>0){
                        $item['name'] = '[副本]'.$certinfo['name'];
                    }
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('cert_borrow', $data['data']['id']);
            $data['lingtype'] = (new CertService())->get_cert_borrow_lingtype();
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_borrow');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_borrow',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('cert_borrow',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_borrow');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_use(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['enginid'=>$param['enginid']])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }
                if(empty($param['yong'])){
                    return outputJson(4000, '请选择用途');
                }
            }

            $param['number'] = (new BianhaoService())->create_cert_use_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_use') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            $param['listnum'] = count($list);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入用证明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    if($bbb['opt'] == '停用'){
                        if(empty($bbb['enddt'])){
                            return outputJson(4000, "用证明细第".$kknum."行请填写使用结束时间");
                        }

                    }

                    $kknum++;
                }
            }




            Db::startTrans();
            try {
                $useid = CertUseModel::insertGetId($param);

                foreach ($list as $item){
                    $item['useid'] = $useid;
                    CertUseListModel::insert($item);
                }

                (new FlowService())->save_bill('cert_use', $useid);

                (new OptService())->relation_file($files,'cert_use',$useid);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_use', $useid);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_use', $useid, '报建用证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$useid]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $ids = $request->param('ids','','string');
            $data['number'] = (new BianhaoService())->create_cert_use_number();
            $data['yong'] = (new OptService())->get_base_info('cert_use_yong');

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $cert_use_list = [];
            $haveArr = [];
            if($data['engininfo']){
                $useArr = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'isreal'=>1,'enginid'=>$data['engininfo']['id']])->find();
                if($useArr){
                    $certuselist = CertUseListModel::where(['useid'=>$useArr['id']])->select()->toArray();
                    foreach ($certuselist as $vvv){
                        $haveArr[] = $vvv['certid'];
                        $certInfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        $vvv['name'] = $certInfo['name'];
                        $vvv['number'] = $certInfo['number'];
                        $vvv['statueuse'] = $certInfo['statueuse'];
                        $vvv['major_name'] = str_replace('[]', ',', $certInfo['major']);
                        $vvv['person_name'] = PersonModel::where(['id'=>$certInfo['personid']])->value('name');
                        $vvv['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                        $cert_use_list[] = $vvv;
                    }
                }
            }


            $list = CertModel::where('id','in', $ids)->select()->toArray();
            foreach ($list as $item){
                if(!in_array($item['id'], $haveArr)){
                    $item['certid'] = $item['id'];
                    $item['major_name'] = str_replace('[]', ',', $item['major']);
                    $item['person_name'] = PersonModel::where(['id'=>$item['personid']])->value('name');
                    $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                    $item['startdt'] = '';
                    $item['enddt'] = '';
                    $item['yudt'] = '';
                    $item['opt'] = 1;
                    $cert_use_list[] = $item;
                }
            }
            $data['list'] = $cert_use_list;


            $data['place'] = (new CertService())->get_cert_place();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_use');
            $data['files'] = (new OptService())->get_file('cert_use');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_use');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('cert_use');
            $data['isnext'] = (new FlowService())->check_isnext('cert_use');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_use(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['enginid'=>$param['enginid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }
                if(empty($param['yong'])){
                    return outputJson(4000, '请选择用途');
                }
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_use') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $list = array_filter($param['list']);
            unset($param['list']);
            $param['listnum'] = count($list);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入用证明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    if($bbb['opt'] == '停用'){
                        if(empty($bbb['enddt'])){
                            return outputJson(4000, "用证明细第".$kknum."行请填写使用结束时间");
                        }

                    }

                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                CertUseModel::update($param);

                CertUseListModel::where('useid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['useid'] = $param['id'];
                    CertUseListModel::insert($item);
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_use', $param['id']);

                }else{
                    (new FlowService())->save_bill('cert_use', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_use',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_use', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_use', $param['id'], '用证申请');
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertUseModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $list = CertUseListModel::where(['useid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $certInfo = CertModel::where(['id'=>$item['certid']])->find();
                $item['name'] = $certInfo['name'];
                $item['number'] = $certInfo['number'];
                $item['statueuse'] = $certInfo['statueuse'];
                $item['major_name'] = str_replace('[]', ',', $certInfo['major']);
                $item['person_name'] = PersonModel::where(['id'=>$certInfo['personid']])->value('name');
                $item['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                $item['opt_name'] = $item['opt'] > 0 ? '在用' : '停用';
            }
            $data['list'] = $list;

            $data['place'] = (new CertService())->get_cert_place();
            $data['yong'] = (new OptService())->get_base_info('cert_use_yong');

            $data['files'] = (new OptService())->get_file('cert_use', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_use');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_use',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('cert_use',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_use');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_employ(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['personid'])) {
                    return outputJson(4000, '请选择持证人');
                }
                if (empty($param['nature'])) {
                    return outputJson(4000, '请选择人员性质');
                }
                if (empty($param['qualification'])) {
                    return outputJson(4000, '请选择聘用资格');
                }
                if (empty($param['pay_type'])) {
                    return outputJson(4000, '请选择聘费支付方式');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入聘费');
                }
                if (empty($param['start_dt'])) {
                    return outputJson(4000, '请选择聘期开始时间');
                }
                if (empty($param['end_dt'])) {
                    return outputJson(4000, '请选择聘期结束时间');
                }
            }


            $param['number'] = (new BianhaoService())->create_cert_employ_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_employ') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = CertEmployModel::insertGetId($param);

                (new FlowService())->save_bill('cert_employ', $id);
                (new OptService())->relation_file($files,'cert_employ',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_employ', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_employ', $id, '聘费合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_cert_employ_number();

            $data['channel'] = (new OptService())->get_base_info('cert_employ_channel');
            $data['purpose'] = (new OptService())->get_base_info('cert_employ_purpose');
            $data['pay_type'] = (new CertService())->get_cert_pay_type();
            $data['nature'] = (new CertService())->get_person_nature();
            $data['qualification'] = (new OptService())->get_base_info('cert_employ_qualification');

            $data['files'] = (new OptService())->get_file('cert_employ');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_employ');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_employ');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('cert_employ');
            $data['isnext'] = (new FlowService())->check_isnext('cert_employ');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_employ(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if (empty($param['personid'])) {
                    return outputJson(4000, '请选择持证人');
                }
                if (empty($param['nature'])) {
                    return outputJson(4000, '请选择人员性质');
                }
                if (empty($param['qualification'])) {
                    return outputJson(4000, '请选择聘用资格');
                }
                if (empty($param['pay_type'])) {
                    return outputJson(4000, '请选择聘费支付方式');
                }
                if ($param['money'] == '') {
                    return outputJson(4000, '请输入聘费');
                }
                if (empty($param['start_dt'])) {
                    return outputJson(4000, '请选择聘期开始时间');
                }
                if (empty($param['end_dt'])) {
                    return outputJson(4000, '请选择聘期结束时间');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_employ') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                CertEmployModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_employ', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_employ', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_employ',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_employ', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_employ', $param['id'], '聘费合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertEmployModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['qualification'] = explode(',',$data['data']['qualification']);
            $data['personidinfo'] = PersonModel::where(['id'=>$data['data']['personid']])->find();

            $data['channel'] = (new OptService())->get_base_info('cert_employ_channel');
            $data['purpose'] = (new OptService())->get_base_info('cert_employ_purpose');
            $data['pay_type'] = (new CertService())->get_cert_pay_type();
            $data['nature'] = (new CertService())->get_person_nature();
            $data['qualification'] = (new OptService())->get_base_info('cert_employ_qualification');

            $data['files'] = (new OptService())->get_file('cert_employ', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_employ');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_employ',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('cert_employ',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_employ');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_empbu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if(empty($param['money'])){
                    return outputJson(4000, '请输入补充聘费');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择聘期开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择聘期结束时间');
                }

                $midinfo = CertEmployModel::where(['id'=>$param['mid']])->find();
                if($param['startdt'] < $midinfo['start_dt']){
                    return outputJson(4000, '聘期开始时间不能小于原合同的聘期开始时间');
                }
                if($param['enddt'] > $midinfo['end_dt']){
                    return outputJson(4000, '聘期结束时间不能大于原合同的聘期结束时间');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_empbu') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = CertEmpbuModel::insertGetId($param);

                (new FlowService())->save_bill('cert_empbu', $id);
                (new OptService())->relation_file($files,'cert_empbu',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_empbu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_empbu', $id, '聘费合同补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $mid = $request->get('mid', 0, 'int');
            $midinfo = CertEmployModel::where(['id'=>$mid])->find();
            $perinfo = PersonModel::where('id',$midinfo['personid'])->find();
            $midinfo['person_name'] = $perinfo['name'];
            $midinfo['person_nature'] = $perinfo['nature'];

            $midinfo['pay_type_name'] = (new CertService())->get_cert_pay_type_name($midinfo['pay_type']);
            $midinfo['channel'] = (new OptService())->get_base_showname('cert_employ_channel', $midinfo['channel']);

            $qualification = array_filter(explode(',',$midinfo['qualification']));
            $midinfo['qualification'] = '';
            foreach ($qualification as $v){
                $midinfo['qualification'] .= empty($midinfo['qualification']) ? (new OptService())->get_base_showname('cert_employ_qualification', $v)
                    : '，'.(new OptService())->get_base_showname('cert_employ_qualification', $v);
            }
            $data['midinfo'] = $midinfo;

            $data['files'] = (new OptService())->get_file('cert_empbu');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('cert_empbu');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_empbu');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('cert_empbu');
            $data['isnext'] = (new FlowService())->check_isnext('cert_empbu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_empbu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0) {
                if(empty($param['money'])){
                    return outputJson(4000, '请输入补充聘费');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择聘期开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择聘期结束时间');
                }

                $mid = CertEmpbuModel::where(['id'=>$param['id']])->value('mid');
                $midinfo = CertEmployModel::where(['id'=>$mid])->find();
                if($param['startdt'] < $midinfo['start_dt']){
                    return outputJson(4000, '聘期开始时间不能小于原合同的聘期开始时间');
                }
                if($param['enddt'] > $midinfo['end_dt']){
                    return outputJson(4000, '聘期结束时间不能大于原合同的聘期结束时间');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'cert_empbu') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                CertEmpbuModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_empbu', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_empbu', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_empbu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_empbu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_empbu', $param['id'], '聘费合同补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertEmpbuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $midinfo = CertEmployModel::where(['id'=>$data['data']['mid']])->find();
            $perinfo = PersonModel::where('id',$midinfo['personid'])->find();
            $midinfo['person_name'] = $perinfo['name'];
            $midinfo['person_nature'] = $perinfo['nature'];
            $midinfo['pay_type_name'] = (new CertService())->get_cert_pay_type_name($midinfo['pay_type']);
            $midinfo['channel'] = (new OptService())->get_base_showname('cert_employ_channel', $midinfo['channel']);

            $qualification = array_filter(explode(',',$midinfo['qualification']));
            $midinfo['qualification'] = '';
            foreach ($qualification as $v){
                $midinfo['qualification'] .= empty($midinfo['qualification']) ? (new OptService())->get_base_showname('cert_employ_qualification', $v)
                    : '，'.(new OptService())->get_base_showname('cert_employ_qualification', $v);
            }
            $data['midinfo'] = $midinfo;

            $data['files'] = (new OptService())->get_file('cert_empbu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_empbu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_empbu',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('cert_empbu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_empbu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入供应方');
                }
            }


            $param['number'] = (new BianhaoService())->create_wu_cai_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'wu_cai') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购物品明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    WuCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_cai', $id);

                (new OptService())->relation_file($files,'wu_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_cai', $id, '物品采购');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_wu_cai_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wu_cai');
            $data['files'] = (new OptService())->get_file('wu_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_cai');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('wu_cai');
            $data['isnext'] = (new FlowService())->check_isnext('wu_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请输入供应方');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'wu_cai') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购物品明细');
                }
            }


            Db::startTrans();
            try {
                WuCaiModel::update($param);

                WuCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    WuCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_cai', $param['id'], '物品采购');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_cai',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('wu_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入领用主题');
                }
            }

            $param['number'] = (new BianhaoService())->create_wu_ling_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'wu_ling') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入领用明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuLingModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['lingid'] = $id;
                    WuLingListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_ling', $id);

                (new OptService())->relation_file($files,'wu_ling',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_ling', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_ling', $id, '物品领用');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_wu_ling_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('wu_ling');
            $data['files'] = (new OptService())->get_file('wu_ling');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_ling');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('wu_ling4');
            $data['isnext'] = (new FlowService())->check_isnext('wu_ling');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入领用主题');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'wu_ling') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入领用明细');
                }
            }


            Db::startTrans();
            try {
                WuLingModel::update($param);

                WuLingListModel::where(['lingid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['lingid'] = $param['id'];
                    WuLingListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_ling', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_ling', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_ling',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_ling', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_ling', $param['id'], '物品领用');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuLingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['runum'] = WuRuListModel::alias('list')
                    ->join('wu_ru ru','ru.id=list.ruid')
                    ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->sum('list.num');
                $item['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$item['name'],'unit'=>$item['unit']])->sum('num');
                $item['chunum'] += WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->where('list.id','neq',$item['id'])
                    ->sum('list.num');
                $item['kucun'] = $item['runum']-$item['chunum'];
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_ling', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_ling');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_ling',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('wu_ling',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_ling');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            foreach ($param as $k=>$v){
                if(strpos($k,'yongtype') !== false){
                    unset($param[$k]);
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'seal_gai') ){return outputJson(4000, $rcfmsg);}
            $list = array_filter($param['list']);
            unset($param['token']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }



            Db::startTrans();
            try {
                $id = SealGaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gaiid'] = $id;
                    SealGaiListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'seal_gai',$id);
                (new FlowService())->save_bill('seal_gai', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_gai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_gai', $id, '日常用印');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['yongtype'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();


            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('seal_gai');
            $data['flowsetname'] = FlowSetModel::get_name('seal_gai');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_gai');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('seal_gai');
            $data['isnext'] = (new FlowService())->check_isnext('seal_gai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            foreach ($param as $k=>$v){
                if(strpos($k,'yongtype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'seal_gai') ){return outputJson(4000, $rcfmsg);}
            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            Db::startTrans();
            try {
                SealGaiModel::update($param);

                SealGaiListModel::where(['gaiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gaiid'] = $param['id'];
                    SealGaiListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'seal_gai',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_gai', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_gai', $param['id']);
                }
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'seal_gai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'seal_gai', $param['id'], '日常用印');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealGaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['yongtype'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();

            $list = SealGaiListModel::where(['gaiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['files'] = (new OptService())->get_file('seal_gai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_gai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_gai',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('seal_gai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_gai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_seal_wai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shi'])){
                    return outputJson(4000, '请输入事项');
                }
                if(empty($param['sealname'])){
                    return outputJson(4000, '请选择外带印章');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'seal_wai') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = SealWaiModel::insertGetId($param);
                (new FlowService())->save_bill('seal_wai', $id);
                (new OptService())->relation_file($files,'seal_wai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_wai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_wai', $id, '印章外带');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['sealname'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('seal_wai');
            $data['flowsetname'] = FlowSetModel::get_name('seal_wai');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_wai');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('seal_wai');
            $data['isnext'] = (new FlowService())->check_isnext('seal_wai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_wai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['shi'])){
                    return outputJson(4000, '请输入事项');
                }
                if(empty($param['sealname'])){
                    return outputJson(4000, '请选择外带印章');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'seal_wai') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                SealWaiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_wai', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_wai', $param['id']);
                }

                (new OptService())->relation_file($files,'seal_wai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_wai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_wai', $param['id'], '印章外带');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealWaiModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }
            $data['sealname'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();

            $data['files'] = (new OptService())->get_file('seal_wai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_wai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_wai',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('seal_wai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_wai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_seal_zhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shenlei'])){
                    return outputJson(4000, '请选择申请类型');
                }
                if(empty($param['isbei'])){
                    return outputJson(4000, '请选择是否备案');
                }
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['yinlei'])){
                    return outputJson(4000, '请选择印章类型');
                }
                if(empty($param['yinxing'])){
                    return outputJson(4000, '请选择印章形状');
                }
                if(empty($param['yinnei'])){
                    return outputJson(4000, '请输入印章内容');
                }
                if(empty($param['shenshi'])){
                    return outputJson(4000, '请输入申请事由(使用范围)');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'seal_zhi') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = SealZhiModel::insertGetId($param);
                (new FlowService())->save_bill('seal_zhi', $id);
                (new OptService())->relation_file($files,'seal_zhi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_zhi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_zhi', $id, '印章刻制');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                //ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['baoer'] = $this->userinfo['realname'];
            $data['shenlei'] = (new XingService())->get_sealzhi_shenlei();
            $data['yinlei'] = (new XingService())->get_sealzhi_yinlei();
            $data['yinxing'] = (new XingService())->get_sealzhi_yinxing();
            $data['yinhan'] = (new XingService())->get_sealzhi_yinhan();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('seal_zhi');
            $data['flowsetname'] = FlowSetModel::get_name('seal_zhi');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_zhi');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('seal_zhi');
            $data['isnext'] = (new FlowService())->check_isnext('seal_zhi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_zhi(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['shenlei'])){
                    return outputJson(4000, '请选择申请类型');
                }
                if(empty($param['isbei'])){
                    return outputJson(4000, '请选择是否备案');
                }
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['yinlei'])){
                    return outputJson(4000, '请选择印章类型');
                }
                if(empty($param['yinxing'])){
                    return outputJson(4000, '请选择印章形状');
                }
                if(empty($param['yinnei'])){
                    return outputJson(4000, '请输入印章内容');
                }
                if(empty($param['shenshi'])){
                    return outputJson(4000, '请输入申请事由(使用范围)');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'seal_zhi') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                SealZhiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_zhi', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_zhi', $param['id']);
                }

                (new OptService())->relation_file($files,'seal_zhi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_zhi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_zhi', $param['id'], '印章刻制');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealZhiModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $data['shenlei'] = (new XingService())->get_sealzhi_shenlei();
            $data['yinlei'] = (new XingService())->get_sealzhi_yinlei();
            $data['yinxing'] = (new XingService())->get_sealzhi_yinxing();
            $data['yinhan'] = (new XingService())->get_sealzhi_yinhan();

            $data['data']['yinleiArr'] = explode(',', $data['data']['yinlei']);
            $data['data']['yinxingArr'] = explode(',', $data['data']['yinxing']);

            $data['files'] = (new OptService())->get_file('seal_zhi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_zhi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_zhi',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('seal_zhi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_zhi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['guidt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
                if(empty($param['purpose'])){
                    return outputJson(4000, '请输入用途');
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dang_jie') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);
            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入借阅档案明细');
                }
            }

            $kknum = 1;
            foreach($list as $key=>$vv){
                $dang = DangModel::where(['id'=>$vv['dangid']])->find();

                $vv['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');
                $vv['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');

                $vv['kejie'] = $dang['num'] - $vv['jienum'];

                if($param['isturn'] > 0){
                    if($vv['num'] > $vv['kejie']){
                        return outputJson(4000, '第'.$kknum.'行借阅数量已超档案可借数量');
                    }
                }
                $kknum++;
            }




            Db::startTrans();
            try {
                $id = DangJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    DangJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('dang_jie', $id);

                (new OptService())->relation_file($files,'dang_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dang_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dang_jie', $id, '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['useer'] = $this->userinfo['realname'];
            $data['depter'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('dang_jie');
            $data['files'] = (new OptService())->get_file('dang_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('dang_jie');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('dang_jie');
            $data['isnext'] = (new FlowService())->check_isnext('dang_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dang_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['guidt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
                if(empty($param['purpose'])){
                    return outputJson(4000, '请输入用途');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'dang_jie') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入借阅档案明细');
                }
            }


            $kknum = 1;
            foreach($list as $key=>$vv){
                $dang = DangModel::where(['id'=>$vv['dangid']])->find();

                $vv['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');
                $vv['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$vv['dangid'])
                    ->sum('list.num');

                $vv['kejie'] = $dang['num'] - $vv['jienum'];

                if($param['isturn'] > 0){
                    if($vv['num'] > $vv['kejie']){
                        return outputJson(4000, '第'.$kknum.'行借阅数量已超档案可借数量');
                    }
                }
                $kknum++;
            }

            Db::startTrans();
            try {
                DangJieModel::update($param);

                DangJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    DangJieListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dang_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('dang_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'dang_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dang_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dang_jie', $param['id'], '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DangJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $list = DangJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $dang = DangModel::where(['id'=>$item['dangid']])->find();
                $item['name'] = $dang['name'];
                $item['number'] = $dang['number'];
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('dang_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dang_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dang_jie',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('dang_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dang_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_tongyong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shilei'])){
                    return outputJson(4000, '请选择事项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入事项摘要');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'tongyong') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            Db::startTrans();
            try {
                $id = TongyongModel::insertGetId($param);
                (new FlowService())->save_bill('tongyong', $id);
                (new OptService())->relation_file($files,'tongyong',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tongyong', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tongyong', $id, '通用审批流程');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('tongyong');
            $data['files'] = (new OptService())->get_file('tongyong');
            $data['isadvan'] = (new FlowService())->check_isadvan('tongyong');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('tongyong');
            $data['isnext'] = (new FlowService())->check_isnext('tongyong');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['shilei'] = (new OptService())->get_base_info('qi_tongyong_shilei');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_tongyong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shilei'])){
                    return outputJson(4000, '请选择事项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入事项摘要');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'tongyong') ){return outputJson(4000, $rcfmsg);}
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                TongyongModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('tongyong', $param['id']);
                }else{
                    (new FlowService())->save_bill('tongyong', $param['id']);
                }

                (new OptService())->relation_file($files,'tongyong',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tongyong', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tongyong', $param['id'], '通用审批流程');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TongyongModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['shilei'] = (new OptService())->get_base_info('qi_tongyong_shilei');

            $data['files'] = (new OptService())->get_file('tongyong', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('tongyong');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('tongyong',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('tongyong',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('tongyong');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入盖章事项摘要');
                }
                if(empty($param['zitype'])){
                    return outputJson(4000, '请选择资料类型');
                }

            }

            foreach ($param as $k=>$v){
                if(strpos($k,'yongtype') !== false){
                    unset($param[$k]);
                }
            }

            $param['number'] = (new BianhaoService())->create_engin_stamp_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_stamp') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);
            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }

            Db::startTrans();
            try {
                $id = EnginStampModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['stampid'] = $id;
                    EnginStampListModel::insert($vv);
                }
                (new OptService())->relation_file($files,'engin_stamp',$id);
                (new FlowService())->save_bill('engin_stamp', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_stamp', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_stamp', $id, '项目资料盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['number'] = (new BianhaoService())->create_engin_stamp_number();
            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['flowsetname'] = FlowSetModel::get_name('engin_stamp');
            $data['files'] = (new OptService())->get_file('engin_stamp');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_stamp');
            $data['imgencpath'] = (new FlowService())->check_imgencpath('engin_stamp');
            $data['isnext'] = (new FlowService())->check_isnext('engin_stamp');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择项目名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入盖章事项摘要');
                }
                if(empty($param['zitype'])){
                    return outputJson(4000, '请选择资料类型');
                }

            }

            foreach ($param as $k=>$v){
                if(strpos($k,'yongtype') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $param['isturn'] > 0 && $rcfmsg = (new OptService())->rela_check_file($files, 'engin_stamp') ){return outputJson(4000, $rcfmsg);}
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }

            }



            Db::startTrans();
            try {
                EnginStampModel::update($param);

                EnginStampListModel::where(['stampid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['stampid'] = $param['id'];
                    EnginStampListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'engin_stamp',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_stamp', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_stamp', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id'], '项目资料盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginStampModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['yongtypemob'] = $item['yongtype'];
                $item['yongtype'] = explode(',', $item['yongtype']);
                $item['yongtype_name'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;

            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_stamp');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_stamp',$data['data']);
            $data['data']['imgencpath'] = (new FlowService())->check_imgencpath_edit('engin_stamp',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_stamp');
            $data['files'] = (new OptService())->get_file('engin_stamp', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




}
